网站建设资讯

NEWS

网站建设资讯

SpringbootFeign文件上传

服务提供者

@SpringBootApplication
public class FeignUploadFirstApplication {
  @RestController
  public class UploadController {

    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
      return file.getOriginalFilename();
    }
  }
  public static void main(String[] args) {
    SpringApplication.run(FeignUploadFirstApplication.class, args);
  }
}

服务消费者

1.feign相关的依赖

目前创新互联建站已为上1000家的企业提供了网站建设、域名、雅安服务器托管网站托管运营、企业网站设计、遵义网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。


      io.github.openfeign.form
      feign-form
      3.0.3
    
    
      io.github.openfeign.form
      feign-form-spring
      3.0.3
    

2.feign配置类

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

3.feign调用接口 注解中加入配置类

@FeignClient(value = "xxx", configuration = FeignMultipartSupportConfig.class)
public interface IUploadService {
    /**
     * 文件上传
     * @param file 文件
     * @return
     */
    @RequestMapping(value = "upload/uppic", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResultModel uppic(@RequestPart(value = "file")MultipartFile file, @RequestParam Map map);

}

4.上传接口

@RestController
public class UploadController {
  @Autowired
   IUploadService uploadService;

  @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
    return uploadService.uppic(file);
  }
}

文件上传 File 转 Multi

FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), ContentType.TEXT_PLAIN.toString(), fileInputStream);
  • 注意:
    1. File 转 MultipartFile 中的第一个参数必须与服务提供者的接口参数一致,即@RequestPart中的value

网站栏目:SpringbootFeign文件上传
文章出自:http://njwzjz.com/article/iicgso.html