springcloud使用feign上传和下载文件

服务端controller

@PostMapping(value = "documentConverter/word2pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Object word2pdf(MultipartFile file, String loginAccount, HttpServletResponse response, HttpServletRequest request) {
        ResponseEntity entity = null;
        
        return entity;
    }

调用端feignclient接口

@FeignClient(value = "asset-doconverter", configuration = AssetDoconverterService.MultipartSupportConfig.class)
public interface AssetDoconverterService {

    @PostMapping(value = "documentConverter/word2pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response word2pdf(@RequestPart MultipartFile file, @RequestParam("loginAccount") String loginAccount);

    @Configuration
    class MultipartSupportConfig {
        @Autowired
        private ObjectFactory messageConverters;

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

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

调用端使用:
把文件转为MultipartFile

    private MultipartFile multipartFileFromInputStream(InputStream inputStream, String fileName) throws IOException {
        FileItemFactory factory = new DiskFileItemFactory();
        FileItem fileItem = factory.createItem("file", null, true, fileName);
        IOUtils.copy(inputStream, fileItem.getOutputStream());
        
        return new CommonsMultipartFile(fileItem);
    }

你可能感兴趣的:(springcloud使用feign上传和下载文件)