javaio
1.本地图片文件转成Base64字符串
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
2.本地图片转成InputStream流对象
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64string);
ByteArrayInputStream stream = new ByteArrayInputStream(bytes1);
3.本地图片转成BufferedImage流(base64string通过1可以得到)
InputStream stream = BaseToInputStream(base64string);
BufferedImage image = ImageIO.read(stream);
4.图片文件保存(imgStrBase64编码的字符串)
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
5.jsp将图片显示到页面
out.clear();
response.setContentType("image/jpg");
BufferedImage image = ImageIO.read(new File("D:\\201804000009-vDr8ZG.JPG"));
ImageIO.write(image, "jpg", response.getOutputStream());
6.jsp或html下载文件
/ 设置浏览器以下载的方式处理该文件名
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[512]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}