上传一个文件,从A接口接收文件,将文件转换为base64字符串,传输到B接口,在B接口中下载文件,保存到固定目录
package cn.git.foreign.controller;
import cn.git.common.exception.ServiceException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* @description: 影像文件通用测试controller
* @program: bank-credit-sy
* @author: lixuchun
* @create: 2024-01-16 11:22:05
*/
@Slf4j
@RestController
@RequestMapping("/foreign")
public class ImageToolController {
/**
* @description: 自定义图像类
* @program: bank-credit-sy
* @author: lixuchun
* @create: 2024-01-16 02:59:15
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class ImageInfo {
/**
* 文件base64编码字符串
*/
private String imageStr;
/**
* 客户自定义信息
*/
private String customInfo;
/**
* 文件名称
*/
private String fileName;
}
/**
* 上传图片
* @param file 文件
* @param customInfo 自定义信息
* @throws IOException
*/
@PostMapping("/image/upload")
public void uploadImage(@RequestParam(value = "file") MultipartFile file, @RequestParam("customInfo") String customInfo) throws IOException {
if (file.isEmpty()) {
throw new ServiceException("mmp,文件空了");
}
// 传输信息封装
ImageInfo imageInfo = new ImageInfo();
String fileName = file.getOriginalFilename();
if (fileName.toLowerCase().endsWith("jpg")) {
imageInfo.imageStr = Base64.getEncoder().encodeToString(file.getBytes());
imageInfo.customInfo = customInfo;
imageInfo.fileName = fileName;
} else {
throw new ServiceException("mmp,文件格式错误");
}
// 发送接口信息到其他接口
HttpResponse loginResponse = HttpRequest.post("localhost:11110/foreign/image/transfer")
.header("X_Token", "LOGIN_TOKEN:d181c46ca92843739b631d355aed1cfd")
.body(JSONObject.toJSONString(imageInfo))
// 超时时间 10 秒
.timeout(10 * 1000)
.execute();
}
/**
* 接收图片
* @param imageInfo 影像信息
* @throws IOException
*/
@PostMapping("/image/transfer")
public void transferImage(@RequestBody ImageInfo imageInfo) throws IOException {
// 获取图片信息
String imageStr = imageInfo.getImageStr();
String customInfo = imageInfo.getCustomInfo();
String fileName = imageInfo.getFileName();
// 打印接收信息
log.info("接收到图片啦[{}]", fileName);
// 解析图片
if (imageStr != null && !imageStr.isEmpty()) {
// 解析图片,生成文件
File newFile = new File("C:\\Users\\Administrator.DESKTOP-40G9I84\\Downloads\\Desktop",
fileName.concat("_").concat(customInfo).concat(".jpg"));
ByteArrayInputStream base64Stream = new ByteArrayInputStream(imageStr.getBytes(StandardCharsets.UTF_8));
Base64InputStream decodedStream = new Base64InputStream(base64Stream);
try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) {
IOUtils.copy(decodedStream, fileOutputStream);
fileOutputStream.flush();
} catch (Exception e) {
log.error("图片解析失败[{}]", fileName);
}
log.info("图片解析成功[{}]", fileName);
}
}
}