使用java将base64码与图片互转!

本篇文章主要介绍了java 后台将base64字符串保存为图片的方法,现在分享给大家,也给大家做个参考。

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 
 8 import Decoder.BASE64Decoder;
 9 import Decoder.BASE64Encoder;
10 
11 public class Base64Utils {
12     /**
13      * 图片转化成base64字符串
14      * @param imgPath
15      * @return
16      */
17     public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
18         String imgFile = imgPath;// 待处理的图片
19         InputStream in = null;
20         byte[] data = null;
21         String encode = null; // 返回Base64编码过的字节数组字符串
22         // 对字节数组Base64编码
23         BASE64Encoder encoder = new BASE64Encoder();
24         try {
25             // 读取图片字节数组
26             in = new FileInputStream(imgFile);
27             data = new byte[in.available()];
28             in.read(data);
29             encode = encoder.encode(data);
30         } catch (IOException e) {
31             e.printStackTrace();
32         } finally {
33             try {
34                 in.close();
35             } catch (IOException e) {
36                 // TODO Auto-generated catch block
37                 e.printStackTrace();
38             }
39         }
40         return encode;
41     }
42 
43     /**
44      * base64字符串转化成图片
45      * 
46      * @param imgData
47      *            图片编码
48      * @param imgFilePath
49      *            存放到本地路径
50      * @return
51      * @throws IOException
52      */
53     @SuppressWarnings("finally")
54     public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
55         if (imgData == null) // 图像数据为空
56             return false;
57         BASE64Decoder decoder = new BASE64Decoder();
58         OutputStream out = null;
59         try {
60             out = new FileOutputStream(imgFilePath);
61             // Base64解码
62             byte[] b = decoder.decodeBuffer(imgData);
63             for (int i = 0; i < b.length; ++i) {
64                 if (b[i] < 0) {// 调整异常数据
65                     b[i] += 256;
66                 }
67             }
68             out.write(b);
69         } catch (FileNotFoundException e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72         } catch (IOException e) {
73             // TODO Auto-generated catch block
74             e.printStackTrace();
75         } finally {
76             out.flush();
77             out.close();
78             return true;
79         }
80     }
81 }
1 public class Test {
2     public static void main(String[] args) throws IOException {
3         String imageStr = Base64Utils.GetImageStr("D://下载/企鹅.jpg");
4         System.out.println(imageStr);
5         Base64Utils.GenerateImage(imageStr, "D://photos/企鹅.jpg");
6     }
7 }

图片编码如下:

原图片为:

转换后为:

原文地址:https://www.cnblogs.com/lfyu/p/8744983.html