使用Feign调用文件上传

使用Feign调用文件上传

目前feign不支持调用文件上传接口需要自行配置来满足feign的调用方式

maven代码块

12345678910  <dependency>    
    <groupId>io.github.openfeign.formgroupId>    
    <artifactId>feign-form-springartifactId>    
    <version>3.2.2version>    
  dependency>    
  <dependency>    
      <groupId>io.github.openfeign.formgroupId>    
      <artifactId>feign-formartifactId>    
      <version>3.2.2version>    
  dependency>  

FeignClient接口代码块

1234567891011121314151617181920  @FeignClient(url = "${manager.base-gateway}"+"/base-file-mgt", name = "base-file-mgt",configuration = UploadFileService.FeignMultipartSupportConfig.class)
  public interface UploadFileService {
  
    @RequestMapping(method = RequestMethod.POST, value = "/api/upload-file",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    FileInfoDTO uploadPayImage(@RequestPart(value = "file") MultipartFile file);
    
      class FeignMultipartSupportConfig {
    
          @Bean
          public Encoder multipartFormEncoder() {
              return new SpringFormEncoder();
          }
    
          @Bean
          public feign.Logger.Level multipartLoggerLevel() {
              return feign.Logger.Level.FULL;
          }
      }
  }
  • feign.Logger.Level.FULL 日志打印类型
  • SpringFormEncoder 字符集转换

打开注解@RequestPart代码

12345678910111213141516171819202122232425262728  @Target(ElementType.PARAMETER)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface RequestPart {

    /**
     * Alias for {@link #name}.
     */
    @AliasFor("name")
    String value() default "";
  
    /**
     * The name of the part in the {@code "multipart/form-data"} request to bind to.
     * @since 4.2
     */
    @AliasFor("value")
    String name() default "";
  
    /**
     * Whether the part is required.
     * 

Defaults to {@code true}, leading to an exception being thrown * if the part is missing in the request. Switch this to * {@code false} if you prefer a {@code null} value if the part is * not present in the request. */ boolean required() default true; }

@RequestPart 才是支持文件上传的注解。{@code “multipart/form-data”}

FileInfoDTO.java

1234567891011121314151617181920212223242526  public class FileInfoDTO implements Serializable {

    private Long id;

    private String tenantId;

    private String createUserId;

    private String filePath;

    private String fileId;

    private ZonedDateTime createDate;

    private Integer disabled;

    private String cosPath;

    private String fileName;

    private ZonedDateTime updateDate;

    private String updateUserId;
    
    //get and set 
    }

###Controller代码块

12345678910  @PostMapping("/orders-pay-img")
    @Timed
    @ApiOperation(value = "上传订单图片", httpMethod = "POST", notes = "上传订单图片")
    public ResponseEntity<FileInfoDTO> createOrder(@RequestParam(value = "file") MultipartFile file,
                                         @ApiParam(name = "filePath", value = "指定文件存储路径(不指定则自动生成文件路径)") @RequestParam(value = "filePath", required = false) String filePath) {

        log.debug("REST request to iamge file : {}", file);
        FileInfoDTO responseEntity= uploadFileService.uploadPayImage(file,tenantId,createUserId,filePath);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(responseEntity));
    }

你可能感兴趣的:(工作笔记)