代碼如下:
1 package com.futuredata.dataservice.util;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.net.HttpURLConnection;
10 import java.net.URL;
11
12
13 import sun.misc.BASE64Decoder;
14 import sun.misc.BASE64Encoder;
15
16 @SuppressWarnings("restriction")
17 public class Base64Utils {
18
19 public static void main(String[] args) throws Exception {
20
21 //本地圖片地址
22 String url = "C:/Users/Administrator/Desktop/628947887489084892.jpg";
23 //在線圖片地址
24 String string = "http://bpic.588ku.com//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg";
25
26 String str = Base64Utils.ImageToBase64ByLocal(url);
27
28 String ste = Base64Utils.ImageToBase64ByOnline(string);
29
30 System.out.println(str);
31
32 Base64Utils.Base64ToImage(str, "C:/Users/Administrator/Desktop/test1.jpg");
33
34 Base64Utils.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg");
35 }
36
37 /**
38 * 本地圖片轉換成base64字符串
39 *
40 * @param imgFile 圖片本地路徑
41 * @return
42 * @author ZHANGJL
43 * @dateTime 2018-02-23 14:40:46
44 */
45 public static String ImageToBase64ByLocal(String imgFile) {// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
46
47
48 InputStream in = null;
49 byte[] data = null;
50
51 // 讀取圖片字節數組
52 try {
53 in = new FileInputStream(imgFile);
54
55 data = new byte[in.available()];
56 in.read(data);
57 in.close();
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 // 對字節數組Base64編碼
62 BASE64Encoder encoder = new BASE64Encoder();
63
64 return encoder.encode(data);// 返回Base64編碼過的字節數組字符串
65 }
66
67
68 /**
69 * 在線圖片轉換成base64字符串
70 *
71 * @param imgURL 圖片線上路徑
72 * @return
73 * @author ZHANGJL
74 * @dateTime 2018-02-23 14:43:18
75 */
76 public static String ImageToBase64ByOnline(String imgURL) {
77 ByteArrayOutputStream data = new ByteArrayOutputStream();
78 try {
79 // 創建URL
80 URL url = new URL(imgURL);
81 byte[] by = new byte[1024];
82 // 創建鏈接
83 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
84 conn.setRequestMethod("GET");
85 conn.setConnectTimeout(5000);
86 InputStream is = conn.getInputStream();
87 // 將內容讀取內存中
88 int len = -1;
89 while ((len = is.read(by)) != -1) {
90 data.write(by, 0, len);
91 }
92 // 關閉流
93 is.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 // 對字節數組Base64編碼
98 BASE64Encoder encoder = new BASE64Encoder();
99 return encoder.encode(data.toByteArray());
100 }
101
102
103 /**
104 * base64字符串轉換成圖片
105 *
106 * @param imgStr base64字符串
107 * @param imgFilePath 圖片存放路徑
108 * @return
109 * @author ZHANGJL
110 * @dateTime 2018-02-23 14:42:17
111 */
112 public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 對字節數組字符串進行Base64解碼並生成圖片
113
114 if (isEmpty(imgStr)) // 圖像數據為空
115 return false;
116
117 BASE64Decoder decoder = new BASE64Decoder();
118 try {
119 // Base64解碼
120 byte[] b = decoder.decodeBuffer(imgStr);
121 for (int i = 0; i < b.length; ++i) {
122 if (b[i] < 0) {// 調整異常數據
123 b[i] += 256;
124 }
125 }
126
127 OutputStream out = new FileOutputStream(imgFilePath);
128 out.write(b);
129 out.flush();
130 out.close();
131
132 return true;
133 } catch (Exception e) {
134 return false;
135 }
136
137 }
138
139 public static boolean isEmpty(String input) {
140 return input == null || input.equals("");
141 }
142
143 }