背景介绍:
有些图片是存放在本地服务器或FTP服务器,并没有图片网络链接,这个时候应该怎么才能通过接口发送到对方的接口里面的?
方案:
采用将图片或文件转换成Base64字符串进行传输,示例代码:
package com.hdyy.timetask.ncToXinWangTimeTask.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
@Component
@Slf4j
public class ImageUtil {
/**
* 把文件转为 base64
*
* @param imagePath 文件全路径名
* @return base64 字符串
*/
public static String imageToBase64(String imagePath) {
try {
File file = new File(imagePath);
FileInputStream fis = new FileInputStream(file);
byte[] imageBytes = new byte[fis.available()];
fis.read(imageBytes);
fis.close();
return Base64.encodeBase64String(imageBytes);
} catch (IOException e) {
log.error("图片转 base64 出现异常", e);
return null;
}
}
/**
* 把文件转为 base64
*
* @param path 文件全路径名
* @return base64 字符串
* @throws Exception 抛出异常
*/
public static String convertBase64(String path) throws Exception {
if (StringUtils.isBlank(path)) {
throw new Exception("path is null");
} else {
return convertBase64(new File(path));
}
}
/**
* 文件转为 Base64
*
* @param file 文件
* @return Base64 字符串
* @throws Exception 抛出异常
*/
public static String convertBase64(File file) throws Exception {
if (Objects.isNull(file)) {
throw new Exception("file is null");
} else if (!file.exists()) {
throw new Exception("file does not exist");
} else {
byte[] data = FileUtils.readFileToByteArray(file);
return Base64.encodeBase64String(data);
}
}
/**
* base64 字符串转图片文件 File
*
* @param code base64 字符串
* @param path 生成图片的全名
* @return 生成的文件
* @throws IOException 抛出异常
*/
public static File base64ToFile(String code, String path) throws IOException {
File file = new File(path);
byte[] data = Base64.decodeBase64(code);
FileUtils.writeByteArrayToFile(file, data);
return file;
}
/**
* 校验图片的宽度和高度是否符合要求(具体可按实际要求修改判断逻辑)
*
* @param path 图片全路径
* @return 符合要求:true;否则:false
*/
public static boolean checkImageWidthAndHeight(String path) {
try {
File imageFile = new File(path);
BufferedImage bufferedImage = ImageIO.read(imageFile);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
log.info("图片:[{}], 宽度:[{}] 像素, 高度:[{}] 像素", path, width, height);
// 假如图片要求是正方形,且不低于 300 * 300,不高于 800 * 800
if (width != height) {
log.warn("图片不是正方形");
return false;
}
if (width < 300) {
log.warn("图片低于 300 * 300,不符合要求");
return false;
}
if (width > 800) {
log.warn("图片高于 800 * 800,不符合要求");
return false;
}
return true;
} catch (IOException e) {
log.error("校验图片规格出现异常", e);
return false;
}
}
public static void main(String[] args) throws Exception {
try {
// 校验图片是否符合规范
String basePath = "D:\\";
String fileName = "test.jpeg";
String fullPath = basePath + File.separator + fileName;
boolean res = checkImageWidthAndHeight(fullPath);
if (!res) {
log.warn("图片不符合规范");
}
// 图片转 base64
String base64Str = convertBase64(fullPath);
log.info("base64 字符串:[{}]", base64Str);
// base64 字符串转图片文件 File
// 生成的图片
String convertPath = basePath + File.separator + "convert.jpg";
base64ToFile(base64Str, convertPath);
// 图片转 base64
log.info("base64 字符串:[{}]", imageToBase64(fullPath));
} catch (IOException e) {
log.error("图片转化测试异常", e);
}
}
}
代码略微老旧,可以采取其他工具类进行替换,上面的代码,需要导入两个依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
选取自网络
评论前必须登录!
注册