欢迎光临
感受代码之美

Springboot通过FeignClient使用MultipartFile上传本地文件

Spring通过FeignClient调用文件上传服务上传本地磁盘文件代码如下:

 //2. 图片解码并保存
String filename = UUID.randomUUID() + ".jpg";//文件名+后缀名

String originalFilename = uploadPath + tbYingqushebei.getCampId() + File.separator + filename;

log.info("图片路径:{}", uploadPath + tbYingqushebei.getCampId() + File.separator + filename);

FileUtils.generateImage(zhoujieruqingaojingjiluAO.getGaojingtupian(), originalFilename);

File originalFile = new File(originalFilename);
DiskFileItem fileItem = new DiskFileItem("file", "image/jpeg", false, originalFile.getName(), (int) originalFile.length(), originalFile.getParentFile());
try (InputStream input = new FileInputStream(originalFile); OutputStream os = fileItem.getOutputStream()) {
    IOUtils.copy(input, os);
    log.info(fileItem.getStoreLocation() + "--getStoreLocation");
} catch (Exception e) {
    throw new IllegalArgumentException("Invalid file:" + e, e);
}

MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

// 调用文件上传服务上传告警图片
R<SysFile> file = remoteFileService.upload(multipartFile);
SysFile sysFile = file.getData();

if (ObjectUtils.isNotEmpty(originalFile) && originalFile.exists()) {
    originalFile.delete();
}
log.info("周杰告警图片url--{}", sysFile.getUrl());

赞(0)
未经允许禁止转载:四个空格 » Springboot通过FeignClient使用MultipartFile上传本地文件

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址