springcloud feign 多文件上传

港控/mmm° 2022-10-02 09:49 337阅读 0赞

1.添加maven引用

  1. <dependency>
  2. <groupId>io.github.openfeign.form</groupId>
  3. <artifactId>feign-form</artifactId>
  4. <version>3.3.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.github.openfeign.form</groupId>
  8. <artifactId>feign-form-spring</artifactId>
  9. <version>3.3.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>commons-fileupload</groupId>
  13. <artifactId>commons-fileupload</artifactId>
  14. <version>1.3.3</version>
  15. </dependency>

2.编写 SpringFormEncoder 处理多个文件上传

  1. package com.bsx.config;
  2. import feign.RequestTemplate;
  3. import feign.codec.EncodeException;
  4. import feign.codec.Encoder;
  5. import feign.form.ContentProcessor;
  6. import feign.form.MultipartFormContentProcessor;
  7. import feign.form.spring.SpringFormEncoder;
  8. import feign.form.spring.SpringManyMultipartFilesWriter;
  9. import feign.form.spring.SpringSingleMultipartFileWriter;
  10. import lombok.val;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.lang.reflect.Type;
  13. import java.util.Collections;
  14. import java.util.Map;
  15. import static feign.form.ContentType.MULTIPART;
  16. /** * @Description: 处理多个文件上传 * @author: ztd * @date 2019/6/14 下午4:26 */
  17. public class FeignSpringFormEncoder extends SpringFormEncoder {
  18. public FeignSpringFormEncoder(Encoder delegate) {
  19. super(delegate);
  20. MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
  21. processor.addWriter(new SpringSingleMultipartFileWriter());
  22. processor.addWriter(new SpringManyMultipartFilesWriter());
  23. }
  24. @Override
  25. public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
  26. if (bodyType != null && bodyType.equals(MultipartFile[].class)) {
  27. MultipartFile[] file = (MultipartFile[]) object;
  28. if(file != null) {
  29. Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
  30. super.encode(data, MAP_STRING_WILDCARD, template);
  31. return;
  32. }
  33. }
  34. super.encode(object, bodyType, template);
  35. }
  36. }

3.Client端配置这个 Encoder Configuration

  1. package com.bsx.client;
  2. import com.bsx.config.FeignSpringFormEncoder;
  3. import feign.codec.Encoder;
  4. import org.springframework.beans.factory.ObjectFactory;
  5. import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  6. import org.springframework.cloud.openfeign.FeignClient;
  7. import org.springframework.cloud.openfeign.support.SpringEncoder;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RequestPart;
  14. import org.springframework.web.multipart.MultipartFile;
  15. /** * @Description: * @author: ztd * @date 2019/6/12 下午7:31 */
  16. @FeignClient(name= "service-hi", configuration = UploadClient.FeignSupportConfig.class)
  17. public interface UploadClient {
  18. /** * 单个文件上传 * @param type * @param file * @return */
  19. @PostMapping(value ="/api/upload",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  20. String upload(@RequestParam(value = "type", required = false) String type,
  21. @RequestPart(value="file", required = false)MultipartFile file);
  22. /** * 多个文件上传 * @param type * @param files * @return */
  23. @PostMapping(value ="/api/multiUpload",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  24. String multiUpload(@RequestParam(value = "type", required = false) String type,
  25. @RequestPart(value="files", required = false)MultipartFile[] files);
  26. @Configuration
  27. class FeignSupportConfig {
  28. @Bean
  29. public Encoder encoder(ObjectFactory<HttpMessageConverters> messageConverters) {
  30. return new FeignSpringFormEncoder(new SpringEncoder(messageConverters)) ;
  31. }
  32. }
  33. }

4.实际的controller处理

  1. package com.bsx.controller;
  2. import org.springframework.http.MediaType;
  3. import org.springframework.web.bind.annotation.*;
  4. import org.springframework.web.multipart.MultipartFile;
  5. /** * @Description: * @author: ztd * @date 2019/6/17 下午2:56 */
  6. @RequestMapping("/api")
  7. @RestController
  8. public class UploadController {
  9. /** * 单个文件上传 * @param type * @param file * @return */
  10. @PostMapping(value ="/upload",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  11. public String upload(@RequestParam(value = "type", required = false) String type,
  12. @RequestPart(value="file", required = false)MultipartFile file) {
  13. System.out.println(file.getOriginalFilename());
  14. return "成功!";
  15. }
  16. /** * 多个文件上传 * @param type * @param files * @return */
  17. @PostMapping(value ="/multiUpload",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  18. public String multiUpload(@RequestParam(value = "type", required = false) String type,
  19. @RequestPart(value="files", required = false)MultipartFile[] files) {
  20. for (MultipartFile file : files) {
  21. System.out.println(file.getOriginalFilename());
  22. }
  23. return "成功!";
  24. }
  25. }

发表评论

表情:
评论列表 (有 0 条评论,337人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringCloud文件

    2.实现图片上传 刚才的新增实现中,我们并没有上传图片,接下来我们一起完成图片上传逻辑。 文件的上传并不只是在品牌管理中有需求,以后的其它服务也可能需要,因此我们创...

    相关 Spring Cloud feign 文件

    先说一下我的应用场景,第三方调用的我的接口,上传若干个文件,我用MultiPartFile\[\] 数组接收,之后我调用其他服务的接口,吧文件发送过去,统一保存。目前存在的问题