2023年4月21日-关于远程feign调用实现文件上传下载

一、客户需求:做一个查询程序,客户提供一个excel模板,将查询结果保存到excel模板中,上传到文件服务,供客户下载使用。

二、代码实现

// 服务A,文件上传
@ApiOperation("上传文件-demo")
@PostMapping(value = "/uploadDemo/{busType}/{billId}")
public ResBean uploadFile(@PathVariable("busType") String busType,
                                          @PathVariable("billId") String billId,
                                          @RequestParam(value = "file")  MultipartFile file
                                         )  {
  
  /**
  ......省略上传逻辑
  */

  return null;
}
// 服务B,feign客户端
package com.xxxx.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Repository
@FeignClient(name = "xxxxx")
public interface FeignClient {
    @PostMapping(value = "/xxxx/xxxx/{busType}/{billId}"
            , produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE
    )
    ResBean uploadFile(@PathVariable("busType") String busType,
                                       @PathVariable("billId") String billId,
                                       @RequestPart("file") MultipartFile file

    );
}
// 服务B,业务层
public ResBean optexcel() throws IOException {
        List<UserEntity> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            UserEntity userEntity = new UserEntity();
            userEntity.setName("张三" + i);
            userEntity.setAge(20 + i);
            userEntity.setTime(new Date(System.currentTimeMillis() + i));
            dataList.add(userEntity);
        }
        //生成excel文档
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户","用户信息"),
                UserEntity.class, dataList);
        MultipartFile file = workbookToCommonsMultipartFile(workbook, "用户.xls");

        ResBean resbean = this.FeignClient.uploadFile(
               "xxxxxx",
               "111222333",
                file
                );
        return resbean;
    }
    public static MultipartFile workbookToCommonsMultipartFile(Workbook workbook, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = factory.createItem("file", "text/plain", true, fileName);
        try {
            OutputStream os = fileItem.getOutputStream();
            workbook.write(os);
            os.close();
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
            return multipartFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
总结:
  坑1:不加如下注解,
  produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
  consumes = MediaType.MULTIPART_FORM_DATA_VALUE
  报错信息:
  Current request is  not a multipart request
  坑2:不使用@RequestPart
  报错信息:the request was rejected because no multipart boundary was found
  坑3:workbookToCommonsMultipartFile中factory.createItem方法中,第一参数,要和@RequestPart注解中value值一致
  不一致报错信息:
  Required request part 'file' is not present
  坑4:上传到服务器的excel重新下载后,打开时,会提示文件损坏
  这时候需要给excel做一下授信,授信成功,就可以正常打开了。
  授信操作参考:https://jingyan.baidu.com/article/c85b7a64a7337d413aac9555.html

热门相关:最强狂兵   大神你人设崩了   霸皇纪   大神你人设崩了   大神你人设崩了