jedis獲取redis中二進(jìn)制圖片轉(zhuǎn)Base64方式
更新時(shí)間:2021年07月15日 08:45:19 作者:清茶淡粥
這篇文章主要介紹了jedis獲取redis中二進(jìn)制圖片轉(zhuǎn)Base64方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
jedis獲取redis圖片 轉(zhuǎn)成Base64
jedis存對象
/** * 序列化 * * @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里面的圖片 轉(zhuǎn)成Base64
/**
* 將字節(jié)轉(zhuǎn)為對象
*/
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字符
String imgBase64Str = Base64Utill.getImgBase64Str(
(byte[])(byte2Object(jedis.get(key.getBytes()))) );
轉(zhuǎn)Base64工具類
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 將字節(jié)轉(zhuǎn)Base64文件
* @param data 要轉(zhuǎn)換的字節(jié)
* @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;
//對字節(jié)數(shù)組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 將字節(jié)轉(zhuǎn)Base64
* @param data 要轉(zhuǎn)換的字節(jié)
* @return Base64字符串
*/
public static String getImgBase64Str(byte[] data) throws IOException {
String fileContentBase64 = null;
String content = null;
//對字節(jié)數(shù)組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 將字節(jié)轉(zhuǎn)Base64文件
* @param file 要轉(zhuǎn)換的文件
* @param fileType 文件類型
* @return Base64字符串
*/
public static String getImgBase64Str(File file) {
String fileContentBase64 = null;
String content = null;
//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
InputStream in = null;
//讀取圖片字節(jié)數(shù)組
try {
in = new FileInputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
in.close();
//對字節(jié)數(shù)組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 將字節(jié)轉(zhuǎn)Base64文件
* @param file 要轉(zhuǎn)換的文件
* @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;
//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
InputStream in = null;
//讀取圖片字節(jié)數(shù)組
try {
in = new FileInputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
in.close();
//對字節(jié)數(shù)組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轉(zhuǎn)字節(jié)
* @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;
}
/**
* 將圖片轉(zhuǎn)換成Base64編碼
* @param imgFile 待處理圖片
* @return
*/
public static String getImgBase64Str(String imgFile) {
// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
InputStream in = null;
byte[] data = null;
// 讀取圖片字節(jié)數(shù)組
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);
}
/**
* 將圖片轉(zhuǎn)換成字節(jié)數(shù)組
* @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;
}
/**
* 對字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
* @param imgStr 圖片數(shù)據(jù)
* @param imgFilePath 保存圖片全路徑地址
* @return
*/
public static boolean generateImage(String imgStr, String imgFilePath) {
//
if (imgStr == null) // 圖像數(shù)據(jù)為空
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) {// 調(diào)整異常數(shù)據(jù)
b[i] += 256;
}
}
// 生成jpg圖片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
b[i] += 256;
}
}
// 生成jpg圖片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用springboot在工具類中讀取配置文件(ClassPathResource)
這篇文章主要介紹了使用springboot在工具類中讀取配置文件(ClassPathResource),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot開發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會(huì)對單體項(xiàng)目進(jìn)行拆分,分解成多個(gè)基于功能的微服務(wù),可能還會(huì)對單個(gè)微服務(wù)進(jìn)行水平擴(kuò)展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07
spring boot idea maven依賴找不到問題處理方法
這篇文章主要介紹了spring boot idea 偶爾maven依賴找不到問題,這里總結(jié)了幾種處理方法,方便嘗試排查,對spring boot idea maven依賴找不到問題感興趣的朋友跟隨小編一起看看吧2023-08-08
java基于servlet實(shí)現(xiàn)文件上傳功能解析
這篇文章主要為大家詳細(xì)介紹了java基于servlet實(shí)現(xiàn)上傳功能,后臺(tái)使用java實(shí)現(xiàn),前端主要是js的ajax實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-05-05
關(guān)于JWT與cookie和token的區(qū)別說明
這篇文章主要介紹了JWT與cookie和token的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

