BASE64Decoder 对文件编码解码
2017-08-09 17:02
246 查看
本文以图片示例
1.对图片进行编码
BASE64Encoder encoder = new BASE64Encoder();
String rootpath = request.getSession().getServletContext()
.getRealPath("/").replace("\\", "/")
+ "../upload/CheckInfo/" + type + "/";
StringBuilder pictureBuffer = new StringBuilder();
rootpath += fileNames[i];
InputStream input = new FileInputStream(new File(rootpath));
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
for (int len = input.read(temp); len != -1; len = input
.read(temp)) {
out.write(temp, 0, len);
pictureBuffer.append(encoder.encode(out.toByteArray()));
// out(pictureBuffer.toString());
out.reset();
}
这里得到
pictureBuffer.toString() 就是最后的编码结果 ,前端支持直接编码显示图片
2.对base64编码进行解码 生成图片
BASE64Decoder decoder = new BASE64Decoder();
String rootpath = request.getSession().getServletContext()
.getRealPath("/").replace("\\", "/")
+ "../" + data.get(i).getfSavePath();
File temp= new File(rootpath);
if (!temp.exists()) {
FileOutputStream write = new FileOutputStream(new File(rootpath));
byte[] decoderBytes = decoder.decodeBuffer(data.get(i).getfBase64Code());
write.write(decoderBytes);
write.close();
}
生成图片的路径就是 rootpath
1.对图片进行编码
BASE64Encoder encoder = new BASE64Encoder();
String rootpath = request.getSession().getServletContext()
.getRealPath("/").replace("\\", "/")
+ "../upload/CheckInfo/" + type + "/";
StringBuilder pictureBuffer = new StringBuilder();
rootpath += fileNames[i];
InputStream input = new FileInputStream(new File(rootpath));
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
for (int len = input.read(temp); len != -1; len = input
.read(temp)) {
out.write(temp, 0, len);
pictureBuffer.append(encoder.encode(out.toByteArray()));
// out(pictureBuffer.toString());
out.reset();
}
这里得到
pictureBuffer.toString() 就是最后的编码结果 ,前端支持直接编码显示图片
2.对base64编码进行解码 生成图片
BASE64Decoder decoder = new BASE64Decoder();
String rootpath = request.getSession().getServletContext()
.getRealPath("/").replace("\\", "/")
+ "../" + data.get(i).getfSavePath();
File temp= new File(rootpath);
if (!temp.exists()) {
FileOutputStream write = new FileOutputStream(new File(rootpath));
byte[] decoderBytes = decoder.decodeBuffer(data.get(i).getfBase64Code());
write.write(decoderBytes);
write.close();
}
生成图片的路径就是 rootpath
相关文章推荐
- 第22天 SequenceInputStream、合并切割、对象的输入输出流、Properties配置文件、.printStream、编码与解码、转换流、递归
- 利用DirectShow实现对视频文件H264编码与解码基类 3
- 发布一个基于BER编码的CDR文件高速解码程序——destroy
- XML中的二进制文件的编码与解码[原创]
- 编码文件为Base64编码形式和解码Base64编码文件
- 利用DirectShow实现对视频文件H264编码与解码基类
- python字符编码解码,文件编码解码。
- java利用Base64编码和解码图片文件
- Java编码字符串,解码字符串,取得文件大小,读文件内容
- 【java基础】采用字节流的文件IO演示,字节流与字符流转换,转换流的编码解码
- java利用Base64编码和解码图片文件
- Huffman对文件编码和解码
- MVC中后台CS文件中对链接进行编码后在前台解码有误处理
- java使用Base64编码和解码的图像文件
- 如何进行编码和通过 Visual C# InfoPath 中以编程方式解码文件附件
- java文件编码和解码
- Python 和 VBScript 对文件编码-解码的方式 (Hex, Base64) 的疑问
- 用C++实现Huffman文件编码和解码(2 总结)
- 利用DirectShow实现对视频文件H264编码与解码基类 2
- eclipse中文件存储的编码格式设置与对编码解码的理解