Erlo

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

2023-04-21 09:30:21 发布   455 浏览  
页面报错/反馈
收藏 点赞

一、客户需求:做一个查询程序,客户提供一个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 dataList = new ArrayList();
        for (int i = 0; i 
总结:
  坑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

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认