后端将图片文件转化为字节数组字符串,并对其进行Base64编码处理
1 public static String getImageStr(String imgFile) {
2 // imgFile = "E:/文档/数据/abc.jpg";
3 InputStream in = null;
4 byte[] data = null;
5 // 读取图片字节数组
6 try {
7 in = new FileInputStream(imgFile);
8 data = new byte[in.available()];
9 in.read(data);
10 in.close();
11 } catch (IOException e) {
12 e.printStackTrace();
13 } catch (FileNotFoundException e) {
14 e.printStackTrace();
15 } catch (java.io.IOException e) {
16 e.printStackTrace();
17 } finally {
18 if (in != null) {
19 try {
20 in.close();
21 } catch (java.io.IOException e) {
22 e.printStackTrace();
23 }
24 }
25 }
26 // 对字节数组Base64编码
27 BASE64Encoder encoder = new BASE64Encoder();
28 return encoder.encode(data);// 返回Base64编码过的字节数组字符串
29 }