File 文件 byte[] 字节 Base64 互转

来源:互联网 发布:csgo低配置优化补丁 编辑:程序博客网 时间:2024/06/05 11:49
   /**     * 通过文件路径将文件转成Base64编码     * @param path 文件路径     * @return base64结果     */    public static String imageToBase64(String path) {        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理        byte[] data = null;        // 读取图片字节数组        try {            InputStream in = new FileInputStream(path);            data = new byte[in.available()];            in.read(data);            in.close();        } catch (IOException e) {            e.printStackTrace();        }        // 对字节数组Base64编码        BASE64Encoder encoder = new BASE64Encoder();        return encoder.encode(data);// 返回Base64编码过的字节数组字符串    }    /**     * 将图片文件转成Base64编码     * @param file 文件     * @return base64码     */     public static imageToBase64(File file) {        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理        byte[] data = null;        // 读取图片字节数组        try {            InputStream in = new FileInputStream(file);            data = new byte[in.available()];            in.read(data);            in.close();        } catch (IOException e) {            e.printStackTrace();        }        // 对字节数组Base64编码        BASE64Encoder encoder = new BASE64Encoder();        return encoder.encode(data);// 返回Base64编码过的字节数组字符串    }    /**     * byte[] 字节转Base64编码     * @param bytes 数组字节     * @return base64码     */     public static stringBase64(byte[] bytes){        BASE64Encoder encoder = new BASE64Encoder();        return encoder.encode(bytes);// 返回Base64编码过的字节数组字符串    }    /**     * 文件转byte[] 数组     * @param file 文件     * @return byte[]     */    public static byte[] imgToByte(File file) {        byte[] data = null;        try {            InputStream in = new FileInputStream(file);            data = new byte[in.available()];            in.read(data);            in.close();        } catch (IOException e) {            e.printStackTrace();        }        return data;    }   /**     * 对字节数组字符串进行Base64解码并生成图片     * @param imgStr base64字符串     * @param imgFilePath 指定输出路径     * @return 布尔值     */public static boolean GenerateImage(String imgStr, String imgFilePath) {         if (imgStr == null) // 图像数据为空               return false;           BASE64Decoder decoder = new BASE64Decoder();           try {               // Base64解码               byte[] bytes = decoder.decodeBuffer(imgStr);               for (int i = 0; i < bytes.length; ++i) {                   if (bytes[i] < 0) {// 调整异常数据                       bytes[i] += 256;                   }               }               // 生成jpeg图片               OutputStream out = new FileOutputStream(imgFilePath);               out.write(bytes);               out.flush();               out.close();               return true;           } catch (Exception e) {               return false;           }       }    /**     * 对字节数组字符串进行Base64解码     * @param imgStr base64字符串     * @return byte[]     */public static byte[] changeBase64(String imgStr) {         String  base = "";        BASE64Decoder decoder = new BASE64Decoder();        byte[] bytes = null;        try {               // Base64解码               bytes = decoder.decodeBuffer(imgStr);               for (int i = 0; i < bytes.length; ++i) {                   if (bytes[i] < 0) {// 调整异常数据                       bytes[i] += 256;                   }               }               // 生成jpeg图片        } catch (Exception e) {              e.printStackTrace();        }          return bytes;    }