您的位置:首页 > 其它

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 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 base64 编码