广告
返回顶部
首页 > 资讯 > 后端开发 > Python >jedis获取redis中二进制图片转Base64方式
  • 250
分享到

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

2024-04-02 19:04:59 250人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

jedis获取Redis图片 转成Base64 jedis存对象 public static byte[] serialize(Object object) { O

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;
}
}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: jedis获取redis中二进制图片转Base64方式

本文链接: https://www.lsjlt.com/news/130506.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作