SpringCloud之Feign声明式服务调用实现文件上传和下载

关于feign声明式服务调用实现文件上传和下载:

记得编写feign服务调用的回调方法,便于发现过程中报告的异常。

  1. 上传为post请求
  2. 异常:Current request is not a multipart request ,将参数注解将@RequestParam 更换为 @RequestPart
  3. 异常:No serializer found for class java.io.FileDescriptor,指定处理请求的提交内容类型consumes = MediaType.MULTIPART_FORM_DATA_VALUE
  4. 异常:no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;],说明结果json转换的时候的异常,增加FeignMultipartSupportConfig配置类
  5. 异常:Could not resolve view with name 'd/dealfile/wordpre/upload' in servlet,上传和下载不需要返回视图,记得使用@Response注解表明该接口的返回类型
  6. 异常:the field file exceeds its maximum permitted size of,SpringBoot嵌入的tomcat限制了请求的文件大小,需要设置spring.servlet.multipart.maxFileSize和spring.servlet.multipart.maxRequestSize
  7. 关于文件下载,我们在client中将文件写入响应流中,使用feign自带的Response对象接收,再通过设置响应体的信息完成下载
  8. 以上每一步的错误都是在编写的过程中发现的,至于为何这样解决,大家可以自行去查找每一步的原因

服务调用方:

    @PostMapping("upload")
    public void upload(@RequestParam("file") MultipartFile[] file){
        uploadClient.upload(file);
    }
    @GetMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response){
        Response download = uploadClient.download();
        Response.Body body = download.body();
        try {
            String fileName = "aaaaa.doc";
            String name = new String(fileName.getBytes(), "iso8859-1");
            response.setContentType("application/msword");//若为excel(application/msexcel)
            response.setHeader("Content-disposition", "attachment;filename=" + name);
            ServletOutputStream outputStream = response.getOutputStream();
            InputStream fileInputStream = body.asInputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

feign接口声明:

@Component
@FeignClient(value = "system-dealfile",contextId = "UploadClient",fallbackFactory = UploadFallback.class)
public interface UploadClient {

    @PostMapping(value="/d/dealfile/wordpre/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public void upload(@RequestPart("file") MultipartFile[] file);

    @GetMapping(value="/d/dealfile/wordpre/download",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public feign.Response download();

}

接口提供方:

    @PostMapping("/d/dealfile/wordpre/upload")
    public void upload(@RequestParam("file") MultipartFile[] file){
        String path = "D:\\kjgh\\a.docx";
        File t = new File(path);
        try {
            file[0].transferTo(t);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/d/dealfile/wordpre/download",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void download(HttpServletRequest request, HttpServletResponse response){
        String path = "D:\\kjgh\\测试.docx";
        try {
            InputStream is = new FileInputStream(new File(path));
            XWPFDocument doc = new XWPFDocument(is);
            ServletOutputStream outputStream = null;
            try {
                String fileName = "下载文档.doc";
                String name = new String(fileName.getBytes(), "iso8859-1");
                response.setContentType("application/msword");//若为excel(application/msexcel)
                response.setHeader("Content-disposition", "attachment;filename=" + name);
                outputStream = response.getOutputStream();
                doc.write(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FeignMultipartSupportConfig:

@Configuration
public class FeignMultipartSupportConfig {

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dong__xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值