package com.rest.ful.utils;
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64ImgUtil {
public static void main(String[] args) {
String strImg = GetImageStr("F:\\2.jpg");
System.out.println(strImg);
GenerateImage(strImg,"F:\\2-1111.jpg");
}
/**
* 图片转化成base64字符串
* @param imgFile - 转换的图片路径
* @return imgStr --图片转换后的二进制字节
*/
public static String GetImageStr(String imgFile) {
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);
}
/**
* 对字节数组字符串进行Base64解码并生成图片
* @param imgStr - 图片二进制字节 imgFilePath-新生成的图片路径
* @return
*/
public static boolean GenerateImage(String imgStr,String imgFilePath) {
if (imgStr == null){
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}