|
@@ -0,0 +1,90 @@
|
|
1
|
+package com.community.huiju.common.hk;
|
|
2
|
+
|
|
3
|
+import java.awt.image.BufferedImage;
|
|
4
|
+import java.io.ByteArrayOutputStream;
|
|
5
|
+import java.io.File;
|
|
6
|
+import java.io.FileOutputStream;
|
|
7
|
+import java.io.IOException;
|
|
8
|
+import java.net.MalformedURLException;
|
|
9
|
+import java.net.URL;
|
|
10
|
+import java.util.Base64;
|
|
11
|
+import java.util.Base64.Decoder;
|
|
12
|
+import java.util.Base64.Encoder;
|
|
13
|
+
|
|
14
|
+import javax.imageio.ImageIO;
|
|
15
|
+
|
|
16
|
+/**
|
|
17
|
+ * Openapi接口调用过程中会用到的一些工具方法
|
|
18
|
+ * @author shengyiling
|
|
19
|
+ *
|
|
20
|
+ */
|
|
21
|
+public class OpenapiUtil {
|
|
22
|
+
|
|
23
|
+ /**
|
|
24
|
+ * BASE64加密网络图片,返回加密之后的字符串
|
|
25
|
+ * @param imageUrl 图片的绝对地址
|
|
26
|
+ * @param extensioName 图片的扩展名,例如 jpg、bmp等
|
|
27
|
+ * @return 加密之后的字符串
|
|
28
|
+ */
|
|
29
|
+ public static String encodeImage2Base64(URL imageUrl, String extensioName){
|
|
30
|
+ ByteArrayOutputStream outputStream = null;
|
|
31
|
+ try {
|
|
32
|
+ BufferedImage bufferedImage = ImageIO.read(imageUrl); //读取网络图片
|
|
33
|
+ outputStream = new ByteArrayOutputStream(); //文件输出流
|
|
34
|
+ ImageIO.write(bufferedImage, extensioName, outputStream);
|
|
35
|
+ } catch (MalformedURLException e1) {
|
|
36
|
+ e1.printStackTrace();
|
|
37
|
+ } catch (IOException e) {
|
|
38
|
+ e.printStackTrace();
|
|
39
|
+ }
|
|
40
|
+ Encoder encoder = Base64.getEncoder();
|
|
41
|
+
|
|
42
|
+ return encoder.encodeToString(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * 将本地图片进行Base64位编码
|
|
47
|
+ *
|
|
48
|
+ * @param imgUrl
|
|
49
|
+ * 图片的url路径,如http://.....xx.jpg
|
|
50
|
+ * @param extensioName 图片的扩展名
|
|
51
|
+ * @return 加密之后的字符串
|
|
52
|
+ */
|
|
53
|
+ public static String encodeImgageToBase64(File imageFile, String extensioName) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
|
|
54
|
+ ByteArrayOutputStream outputStream = null;
|
|
55
|
+ try {
|
|
56
|
+ BufferedImage bufferedImage = ImageIO.read(imageFile);
|
|
57
|
+ outputStream = new ByteArrayOutputStream();
|
|
58
|
+ ImageIO.write(bufferedImage, extensioName, outputStream);
|
|
59
|
+ } catch (MalformedURLException e1) {
|
|
60
|
+ e1.printStackTrace();
|
|
61
|
+ } catch (IOException e) {
|
|
62
|
+ e.printStackTrace();
|
|
63
|
+ }
|
|
64
|
+ // 对字节数组Base64编码
|
|
65
|
+ Encoder encoder = Base64.getEncoder();
|
|
66
|
+
|
|
67
|
+ return encoder.encodeToString(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+ /**
|
|
72
|
+ * 将Base64位编码的图片进行解码,并保存到指定目录
|
|
73
|
+ * @param base64Str 利用base64加密之后的字符串
|
|
74
|
+ * @param path 文件解密之后存放的地址 例如:D://
|
|
75
|
+ * @param imgName 文件解密之后命名的名称 例如: test.jpg
|
|
76
|
+ */
|
|
77
|
+ public static void decodeBase64ToImage(String base64Str, String path,
|
|
78
|
+ String imgName) {
|
|
79
|
+ Decoder decoder = Base64.getDecoder();
|
|
80
|
+ try {
|
|
81
|
+ FileOutputStream write = new FileOutputStream(new File(path
|
|
82
|
+ + imgName));
|
|
83
|
+ byte[] decoderBytes = decoder.decode(base64Str);
|
|
84
|
+ write.write(decoderBytes);
|
|
85
|
+ write.close();
|
|
86
|
+ } catch (IOException e) {
|
|
87
|
+ e.printStackTrace();
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+}
|