feign文件传输

pom文件添加依赖


			io.github.openfeign.form
			feign-form
			3.3.0
		
		
			io.github.openfeign.form
			feign-form-spring
			3.3.0
		
		
			commons-fileupload
			commons-fileupload
			1.3.3

被调用的feign的控制层添加配置

 class MyConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }

requestmapping添加属性
MediaType.MULTIPART_FORM_DATA_VALUE
文件使用@RequestPart注解接收
添加完成后大致为这样


@FeignClient(value = "mengruankj-education-system-service" )
public interface IFeignFileUpload {

    @RequestMapping(value = "feign/file/uploadMediaDocFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,method = RequestMethod.POST)
    Boolean uploadMediaDoc(@RequestPart("mediaDocFile") MultipartFile mediaDocFile,@RequestParam("storagePath") String storagePath);

    @RequestMapping(value = "feign/file/uploadPicFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,method = RequestMethod.POST)
    Boolean uploadPicFile(@RequestPart("picFile") MultipartFile picFile,@RequestParam("storagePath") String storagePath);

    @RequestMapping(value = "feign/file/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,method = RequestMethod.POST)
    Boolean uploadFile(@RequestPart("file")MultipartFile file,@RequestParam("storagePath")  String storagePath);

    class MyConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

如果是控制层的MultipartFile直接拿过来的需要注意文件名与@RequestPart里的参数一致
就是说你直接把上传的MultipartFile传输,则文件名应为上传的文件名

最后提供一个工具类file转MultipartFile

 /** 
    * @Description: file转MultipartFile 
    * @Param:  
    * @return:  
    * @Author: zzy 
    * @Date: 2020/8/24 
    */ 
    public static MultipartFile getMulFileByPath(File file,String paramName) {
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
              MultipartFile multipartFile = new MockMultipartFile(paramName, file.getName(), "text/plain", input);
            return multipartFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

需添加pom依赖

  <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId> 
        </dependency>

你可能感兴趣的:(后台)