文件和base64流的相互转换
来源:互联网 发布:淘宝红包充话费 编辑:程序博客网 时间:2024/06/07 06:05
import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 文件与base64的互相转换操作
*/
public class testFile {
public static void main(String[] args) {
testFile t = new testFile();
try {
String ret = t.encodeBase64File("d://IE和火狐js或css差异.docx");
System.err.println(ret);
t.decoderBase64File(ret, "d://ghsTest/retFile.docx", "d://ghsTest/");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将文件转成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,String catalogue)
throws Exception {
File file = new File(catalogue);
if(file.exists()==false){
file.mkdirs();
}
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 文件与base64的互相转换操作
*/
public class testFile {
public static void main(String[] args) {
testFile t = new testFile();
try {
String ret = t.encodeBase64File("d://IE和火狐js或css差异.docx");
System.err.println(ret);
t.decoderBase64File(ret, "d://ghsTest/retFile.docx", "d://ghsTest/");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将文件转成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,String catalogue)
throws Exception {
File file = new File(catalogue);
if(file.exists()==false){
file.mkdirs();
}
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}
1 0
- 文件和base64流的相互转换
- Base64和Bitmap的相互转换
- Bitmap和Base64的相互转换
- Bitmap和Base64的相互转换
- base64和byte[]相互转换
- java之文件与base64字符之间的相互转换
- java之文件与base64字符之间的相互转换
- java之文件与base64字符之间的相互转换
- java之文件与base64字符之间的相互转换
- 录音文件与Base64编码相互转换的方法
- 录音文件与Base64编码相互转换的方法
- 文件和字节流的相互转换
- 文件和字节流的相互转换
- 文件和字节流的相互转换
- 文件和字节流的相互转换
- Image和Base64 String之间的相互转换
- NSString和NSData通过Base64编码的相互转换
- java 实现 :Base64字符串 和 图片 之间的相互转换
- Storm Trident 教程
- 字节转换成二进制
- 教你写Android网络框架
- Sicily 4835. Numbersrebmun
- 在PHP中使用键值对数组构造JSON数据
- 文件和base64流的相互转换
- Remote Desktop -- Access Ubuntu 14.04 from Windows 7
- 转载:ASP.NET MVC4 NInject 使用示例教程
- 【SzNOI语法百题】【d018】三数排序
- 【网络模型】一个客户一个线程
- 我的IMU第七天
- jsoncpp链接错误解决方法
- windows笔记-跨越进程边界共享内核对象【复制对象句柄】
- Linux的文件权限和目录配置(三)