java之文件与base64字符之间的相互转换
2015-12-08 10:35
357 查看
package cn.xuanyuan.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class FileUitl { /** * 将文件转成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(); } } }
相关文章推荐
- VC中BASE64编码和解码使用详解
- Base64编码解码原理及C#编程实例
- C#编写的Base64加密和解密类
- JS 实现BASE64_ENCODE和BASE64_DECODE(实例代码)
- C# Base64编码函数
- ASP BASE64加解密(亲测可用)
- c# Base64编码和图片的互相转换代码
- Js base64 加密解密介绍
- php中base64_decode与base64_encode加密解密函数实例
- C#解码base64编码二进制数据的方法
- Shell脚本中不同进制数据转换的例子(二进制、八进制、十六进制、base64)
- php基于base64解码图片与加密图片还原实例
- C#对二进制数据进行base64编码的方法
- javascript从image转换为base64位编码的String
- javascript实现base64 md5 sha1 密码加密
- JS实现的3des+base64加密解密算法完整示例
- C#实现基于Base64的加密解密类实例
- 简洁实用的Java Base64编码加密异常处理类代码
- java常用工具类之DES和Base64加密解密类
- 基于Java实现的Base64加密、解密原理代码