jedis获取redis中二进制图片转Base64方式

时间:2021-07-12 05:18:18

jedis获取redis图片 转成Base64

jedis存对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/** * 序列化 * * @param object * @return */
public static byte[] serialize(Object object) {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        byte[] bytes = baos.toByteArray();
        return bytes;
    } catch (Exception e) {
        
    } return null;
}
/** * 反序列化 * * @param bytes * @return */
public static Object unserialize(byte[] bytes) {
    ByteArrayInputStream bais = null;
    try {
  ObjectInputStream inputStream;
        inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object obj = inputStream.readObject();
    } catch (Exception e) {
        
    }
    return null;
}

jedis获取redis里面的图片 转成Base64

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
  * 将字节转为对象
  */
 public Object byte2Object(byte[] bytes) {
        if (bytes == null || bytes.length == 0)
            return null;
        try {
            ObjectInputStream inputStream;
            inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Object obj = inputStream.readObject();
            return obj;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

获取Base64字符

?
1
2
String imgBase64Str = Base64Utill.getImgBase64Str( 
    (byte[])(byte2Object(jedis.get(key.getBytes())))  );

转Base64工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.stream.FileImageInputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Utill {
 /**
   *
   * @Description 将字节转Base64文件
   * @param data 要转换的字节
   * @param fileType 文件类型
   * @return Base64字符串
   */
 public static String getImgBase64Str(byte[] data, String fileType) throws IOException {
     String fileContentBase64 = null;
     String base64Str = "data:" + fileType + ";base64,";
     String content = null;     
         //对字节数组Base64编码
         if (data == null || data.length == 0) {
             return null;
         }
         BASE64Encoder encoder = new BASE64Encoder();
         content = encoder.encode(data);
         if (content == null || "".equals(content)) {
             return null;
         }
         fileContentBase64 = base64Str + content;
     return fileContentBase64;
 }
 
 
 /**
   *
   * @Description 将字节转Base64
   * @param data 要转换的字节
   * @return Base64字符串
   */
 public static String getImgBase64Str(byte[] data) throws IOException {
     String fileContentBase64 = null;
     String content = null;     
         //对字节数组Base64编码
         if (data == null || data.length == 0) {
             return null;
         }
         BASE64Encoder encoder = new BASE64Encoder();
         content = encoder.encode(data);
         
         if (content == null || "".equals(content)) {
             return null;
         }
         fileContentBase64=content;
     return fileContentBase64;
 }
 
 /**
   *
   * @Description 将字节转Base64文件
   * @param file  要转换的文件
   * @param fileType 文件类型
   * @return Base64字符串
   */
 public static String getImgBase64Str(File file)  {
     String fileContentBase64 = null;
  
     String content = null;
     //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     InputStream in = null;
     //读取图片字节数组
     try {
         in = new FileInputStream(file);
         byte[] data =  new byte[in.available()];
         in.read(data);
         in.close();
         //对字节数组Base64编码
         if (data == null || data.length == 0) {
             return null;
         }
         BASE64Encoder encoder = new BASE64Encoder();
         content = encoder.encode(data);
         if (content == null || "".equals(content)) {
             return null;
         }
         return encoder.encode(data);
     } catch (IOException e) {
         e.printStackTrace();
     } finally {
         if (in != null) {
             try {
     in.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
         }
     }
     return fileContentBase64;
 }
 
 /**
   *
   * @Description 将字节转Base64文件
   * @param file  要转换的文件
   * @param fileType 文件类型
   * @return Base64字符串
   */
 public static String getImgBase64Str(File file, String fileType) throws IOException {
     String fileContentBase64 = null;
     String base64Str = "data:" + fileType + ";base64,";
     String content = null;
     //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     InputStream in = null;
     //读取图片字节数组
     try {
         in = new FileInputStream(file);
         byte[] data =  new byte[in.available()];
         in.read(data);
         in.close();
         //对字节数组Base64编码
         if (data == null || data.length == 0) {
             return null;
         }
         BASE64Encoder encoder = new BASE64Encoder();
         content = encoder.encode(data);
         if (content == null || "".equals(content)) {
             return null;
         }
         fileContentBase64 = base64Str + content;
     } catch (IOException e) {
         e.printStackTrace();
     } finally {
         if (in != null) {
             in.close();
         }
     }
     return fileContentBase64;
 }
 /**
   *
   * @Description base64转字节
   * @param base64Str 字符串
   * @return byte[] 
   */
  public static byte[] base64String2Byte(String base64Str){ 
         BASE64Decoder decoder = new BASE64Decoder();
         byte[] b = null;
          try {
              b = decoder.decodeBuffer(base64Str);
              for (int i = 0; i < b.length; ++i) {
                  if (b[i] < 0) {
                      b[i] += 256;
                  }
             }
         } catch (IOException e) {
             e.printStackTrace();
         
         return b; 
     }
  /**
      * 将图片转换成Base64编码
      * @param imgFile 待处理图片
      * @return
      */
    public static String getImgBase64Str(String imgFile) {
        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
  
    /**
      * 将图片转换成字节数组
      * @param imgFile 待处理图片
      * @return
      */
    public  byte[] image2byte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        }
        catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        }
        catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }
     
    /**
   * 对字节数组字符串进行Base64解码并生成图片
   * @param imgStr 图片数据
   * @param imgFilePath 保存图片全路径地址
   * @return
   */
    public static boolean generateImage(String imgStr, String imgFilePath) {
        //
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
     }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
b[i] += 256;
}
}
// 生成jpg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_33758782/article/details/117454310