import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class FileUitl {
/**
* 將文件轉(zhuǎn)成base64 字符串
* @param path文件路徑
* @return *
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
/**
* 將base64字符解碼保存文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void decoderBase64File(String base64Code, String targetPath) throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
/**
* 將base64字符保存文本文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void toFile(String base64Code, String targetPath) throws Exception {
byte[] buffer = base64Code.getBytes();
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
public static void main(String[] args) {
try {
String base64Code = encodeBase64File("D:/0101-2011-qqqq.tif");
System.out.println(base64Code);
decoderBase64File(base64Code, "D:/2.tif");
toFile(base64Code, "D:\\three.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
java之文件與base64字符之間的相互轉(zhuǎn)換
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贯被,“玉大人癞谒,你說我怎么就攤上這事∪姓ィ” “怎么了弹砚?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長枢希。 經(jīng)常有香客問我桌吃,道長,這世上最難降的妖魔是什么苞轿? 我笑而不...
- 正文 為了忘掉前任茅诱,我火速辦了婚禮,結(jié)果婚禮上搬卒,老公的妹妹穿的比我還像新娘瑟俭。我一直安慰自己,他們只是感情好契邀,可當(dāng)我...
- 文/花漫 我一把揭開白布摆寄。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪微饥。 梳的紋絲不亂的頭發(fā)上逗扒,一...
- 文/蒼蘭香墨 我猛地睜開眼始锚,長吁一口氣:“原來是場噩夢啊……” “哼刽酱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起疼蛾,我...
- 正文 年R本政府宣布,位于F島的核電站新锈,受9級特大地震影響甲脏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜妹笆,卻給世界環(huán)境...
- 文/蒙蒙 一块请、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拳缠,春花似錦墩新、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽绵疲。三九已至,卻和暖如春切省,著一層夾襖步出監(jiān)牢的瞬間最岗,已是汗流浹背帕胆。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 寫在前面 上次在簡書上第一次發(fā)表文章多了30位朋友關(guān)注和收到80多個喜歡讓我受寵若驚。我當(dāng)時打算以后要多寫一些有價...
- 在履行承諾的過程中 總會出現(xiàn)一些 預(yù)料不到的阻礙 讓人心煩意亂, 你和我 口頭約定的內(nèi)容 也應(yīng)該 順著局勢的發(fā)展 ...