jedis获取redis中二进制图片转Base64方式
短信预约 -IT技能 免费直播动态提醒
jedis获取redis图片 转成Base64
jedis存对象
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;
}
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
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()))) );
转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 {
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;
}
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;
}
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;
}
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;
}
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;
}
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);
}
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;
}
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;
}
}
}
b[i] += 256;
}
}
// 生成jpg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341