项目中有时会需要实现base64与图片的相互转化,首先需要引入commons-codec.jar
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
java代码如下:
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import org.apache.commons.codec.binary.Base64;
/**
* @description: 将网络图片转换成base64码
* @Author:
* @Date: 2019/10/19 0019 11:50
*/
@Log4j2
@Component
public class ImageToBase64Util {
/**
* 将网络图片转换成base码
* @param imgURL
* @return
* @throws Exception
*/
public String doGetImageBase64StrFromUrl(String imgURL) throws Exception{
byte[] data = null;
InputStream inStream = null;
try {
// 创建URL
URL url = new URL(imgURL);
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
inStream = conn.getInputStream();
data = new byte[inStream.available()];
inStream.read(data);
} catch (IOException e) {
e.printStackTrace();
String exMsg = MessageFormat.format("通过图片URL获取Base64字符串时发生异常:{0}", e.getMessage());
log.error(exMsg);
} finally {
try {
if (null != inStream) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
String exMsg = MessageFormat.format("通过图片URL获取Base64字符串关闭InputStream时发生异常:{0}", e.getMessage());
log.error(exMsg);
}
}
// 对字节数组Base64编码
byte[] encodeBase64 = Base64.encodeBase64(data);
// 返回Base64编码过的字节数组字符串
String imageBase64String = new String(encodeBase64);
return imageBase64String;
}
/***
* 将本地图片转换成base吗
* @param imgPath
* @return
*/
public String doGetImageStrFromPath(String imgPath){
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
} catch (IOException e) {
e.printStackTrace();
String exMsg = MessageFormat.format("通过图片文件路径获取Base64字符串时发生异常:{0}", e.getMessage());
log.error(exMsg);
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
String exMsg = MessageFormat.format("通过图片URL获取Base64字符串关闭InputStream时发生异常:{0}", e.getMessage());
log.error(exMsg);
}
}
// 对字节数组Base64编码
byte[] encodeBase64 = Base64.encodeBase64(data);
// 返回Base64编码过的字节数组字符串
String imageBase64String = new String(encodeBase64);
if (null == imageBase64String || imageBase64String.length() == 0) {
log.error("图片Base64字符串为空,请检查造成原因.");
}
return imageBase64String;
}
/**
* 将base64转换成图片
* @param imgBase64String
* @param dir
* @param fileName
* @return
*/
public static void doSaveBase64AsImageFile(String imgBase64String, String dir, String fileName) {
// 图像数据为空
if (imgBase64String == null) {
log.error("将Base64字符串保存为本地图片文件时发生异常:图片Base64字符串不能为空");
}
// Base64解码
byte[] bytes = Base64.decodeBase64(new String(imgBase64String).getBytes());
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
// 调整异常数据
bytes[i] += 256;
}
}
OutputStream out = null;
try {
File directory = new File(dir);
// 判断文件目录是否存在
if (!directory.exists() && directory.isDirectory()) {
// 如果不存在则创建目录
directory.mkdirs();
}
// 本地图片文件保存路径
String imageSaveToFilePath = dir + File.separator + fileName;
out = new FileOutputStream(imageSaveToFilePath);
out.write(bytes);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error("请检查本地图片文件保存路径是否存在:{0}", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.error("图片Base64字符串保存为本地图片文件时发生异常:{0}", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
log.error("图片Base64字符串保存为本地图片文件时发生异常:{0}", e.getMessage());
} finally {
try {
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
log.error("图片Base64字符串保存为本地图片文件关闭OutputStream时发生异常:{0}", e.getMessage());
}
}
}
}