最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

最新評論

喜德县| 襄樊市| 宁津县| 兴隆县| 中西区| 富蕴县| 永兴县| 荥阳市| 崇文区| 昌图县| 新沂市| 杨浦区| 永泰县| 甘肃省| 张北县| 湟源县| 阿拉善右旗| 法库县| 沙河市| 莱阳市| 柞水县| 辉南县| 布拖县| 札达县| 广西| 鄂托克前旗| 阜新市| 迭部县| 永昌县| 孟连| 洱源县| 吴堡县| 潞城市| 新河县| 横山县| 洛宁县| 敦化市| 新源县| 军事| 定结县| 宣武区|