当前位置 : 主页 > 编程语言 > java >

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

来源:互联网 收集:自由互联 发布时间:2021-08-21
jedis获取redis图片 转成Base64 jedis存对象 /** * 序列化 * * @param object * @return */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOu

jedis获取redis图片 转成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里面的图片 转成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 {
 /** 
   * 
   * @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;
        }
     }
 }
b[i] += 256;
}
}
// 生成jpg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

网友评论