Java实现文件和base64流的相互转换功能示例
本文实例讲述了Java实现文件和base64流的相互转换功能。分享给大家供大家参考,具体如下:
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
/**
*文件与base64的互相转换操作
*/
publicclasstestFile{
publicstaticvoidmain(String[]args){
testFilet=newtestFile();
try{
Stringret=t.encodeBase64File("d://IE和火狐js或css差异.docx");
System.err.println(ret);
t.decoderBase64File(ret,"d://ghsTest/retFile.docx","d://ghsTest/");
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*将文件转成base64字符串
*
*@parampath文件路径
*@return*
*@throwsException
*/
publicstaticStringencodeBase64File(Stringpath)throwsException{
Filefile=newFile(path);
FileInputStreaminputFile=newFileInputStream(file);
byte[]buffer=newbyte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
returnnewBASE64Encoder().encode(buffer);
}
/**
*将base64字符解码保存文件
*
*@parambase64Code
*@paramtargetPath
*@throwsException
*/
publicstaticvoiddecoderBase64File(Stringbase64Code,StringtargetPath,Stringcatalogue)
throwsException{
Filefile=newFile(catalogue);
if(file.exists()==false){
file.mkdirs();
}
byte[]buffer=newBASE64Decoder().decodeBuffer(base64Code);
FileOutputStreamout=newFileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}
PS:这里再推荐几款加密解密相关在线工具供大家参考使用:
线编码转换工具(utf-8/utf-32/Punycode/
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下