zoukankan      html  css  js  c++  java
  • springboot微服务间上传文件

    springboot微服务之间通常使用feign进行接口调用,那么在此基础上文件上传如何操作呢?

    最近项目中碰到了这种需求,此处分享一下使用心得,希望对大家有帮助!!!

    一、我这里使用的相关包如下,其它的大家视情况而定

             <dependency>
                <groupId>io.github.openfeign.form</groupId>
                <artifactId>feign-form-spring</artifactId>
                <version>3.3.0</version>
            </dependency>                        

      二、配置文件相关配置如下:

    spring:
      servlet:
        multipart:
          enabled: true # 启用上传处理,默认是true
          file-size-threshold: 1MB # 当上传文件达到1MB的时候进行磁盘写入
          max-request-size: 100MB # 设置最大的请求文件的大小
          max-file-size: 2MB # 设置单个文件的最大长度

      三、启动加载相关配置

    @Configuration
    public class FeignMultipartSupportConfig {
    
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;
    
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

      四、调用方Feign接口

    @FeignClient(name = "ms-merchant-basis-server")
    public interface AttachmentFegin {
        @PostMapping(value = "/api/upload/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        @ApiOperation(value = "上传图片,文件不能为空,大小不能超过10M")
        PicVo uploadImage(@RequestPart("file") MultipartFile file);
    }

        注:

          consumes:必须使用 MediaType.MULTIPART_FORM_DATA_VALUE
          produces:接口多参数时增加此配置 MediaType.APPLICATION_JSON_UTF8_VALUE
          @RequestPart:服务调用方接口文件类型使用此注解 

      五、被调用方Controller接口

        @RequestMapping(value = "/api/upload/image", method = RequestMethod.POST)
        @ResponseBody
        @ApiOperation(value = "上传图片,文件不能为空,大小不能超过10M")
        public void uploadImage(@RequestParam("file") MultipartFile file)  {}

                注:

          @RequestParam :此注解包含@RequestPart,但是@RequestPart不包含@RequestParam

    最后给大家来张宅男图片缓解一下眼睛

  • 相关阅读:
    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
    Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
    HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
    Eclipse中设置编码的方式
    javadoc简介
    正则表达式:网页爬虫:从TXT中获取邮箱地址(获取的练习,缺点:一行只能匹配一个)
    Linux系统中yum 命令讲解
    查看CentOS版本信息
    Linux系统下安装JDK
    Linux基础性笔记
  • 原文地址:https://www.cnblogs.com/jwdd/p/14838604.html
Copyright © 2011-2022 走看看