• feign传输文件


    一、对于单个文件上传

    1. @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/file/upload")
    2. ResponseVO<UploadFileVO> upload(@RequestPart MultipartFile picFile,@RequestParam Integer count,@RequestParam String type,@RequestParam String service);
    3. File image1 = new File(imagePath1);
    4. MockMultipartFile imageMultiFile1 = new MockMultipartFile(image1.getName(),new FileInputStream(image1));
    5. ResponseVO<UploadFileVO> imageResponse1 = fileClient.upload(imageMultiFile1,1,"file","upload");

    @RequestPart主要用来处理content-type为 multipart/form-data 或 multipart/mixed stream 发起的请求,可以获取请求中的参数,包括普通文本、文件或复杂对象比如json、xml等,针对json等复杂对象。

    @RequestParam也同样支持multipart/form-data请求,@RequestParam适用于name-value String类型的请求,@RequestPart适用于复杂的请求域(像JSON,XML)

    上例中的@RequestParam参数可以通过以下方法获取

    String type = request.getParameter("type");

    二、对于多个文件上传

    2.1、多文件编码解析,重写encode方法,处理多文件

    1. import feign.RequestTemplate;
    2. import feign.codec.EncodeException;
    3. import feign.codec.Encoder;
    4. import feign.form.ContentType;
    5. import feign.form.MultipartFormContentProcessor;
    6. import feign.form.spring.SpringFormEncoder;
    7. import feign.form.spring.SpringManyMultipartFilesWriter;
    8. import feign.form.spring.SpringSingleMultipartFileWriter;
    9. import org.springframework.web.multipart.MultipartFile;
    10. import java.lang.reflect.Type;
    11. import java.util.Collections;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. /**
    15. * @Author: nzy
    16. * @Description:
    17. * @Date:Create:in 2022/8/5 10:27
    18. * @Modified By:
    19. */
    20. public class SpringMultipartEncoder extends SpringFormEncoder {
    21. public SpringMultipartEncoder() {
    22. this(new Default());
    23. }
    24. public SpringMultipartEncoder(Encoder delegate) {
    25. super(delegate);
    26. MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
    27. processor.addWriter(new SpringSingleMultipartFileWriter());
    28. processor.addWriter(new SpringManyMultipartFilesWriter());
    29. }
    30. @Override
    31. public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
    32. if (bodyType != null && bodyType.equals(MultipartFile.class)) {
    33. MultipartFile file = (MultipartFile) object;
    34. if (file != null) {
    35. Map data = Collections.singletonMap(file.getName(), object);
    36. super.encode(data, MAP_STRING_WILDCARD, template);
    37. return;
    38. }
    39. } else if (bodyType != null && bodyType.equals(MultipartFile[].class)) {
    40. if(object!=null) {
    41. MultipartFile[] files = (MultipartFile[]) ((MultipartFile[]) object);
    42. Map data = new HashMap<>();
    43. for(int i=1;i<=files.length;i++) {
    44. data.put("file"+i,files[i-1]);
    45. }
    46. super.encode(data, MAP_STRING_WILDCARD, template);
    47. return;
    48. }
    49. }
    50. super.encode(object, bodyType, template);
    51. }
    52. }

      2.2、config配置文件

    1. import feign.codec.Encoder;
    2. import org.springframework.beans.factory.ObjectFactory;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.annotation.Configurable;
    5. import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
    6. import org.springframework.cloud.openfeign.support.SpringEncoder;
    7. import org.springframework.context.annotation.Bean;
    8. /**
    9. * @Author: nzy
    10. * @Description:
    11. * @Date:Create:in 2022/8/5 10:27
    12. * @Modified By:
    13. */
    14. @Configurable
    15. public class MultipartSupportConfig {
    16. @Autowired
    17. private ObjectFactory messageConverters;
    18. @Bean
    19. public Encoder feignFormEncoder() {
    20. return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
    21. }
    22. }

     2.3、在feignclient上配置configuration 属性为自定义的Encoder,这个config仅影响设置的feign

    1. @FeignClient(value="${file.microServiceName}",fallback = FileClientFallback.class,contextId = "dormClient",configuration = SpringMultipartEncoder.class)
    2. public interface FileClient {
    3. @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/file/upload")
    4. ResponseVO upload(@RequestPart MultipartFile[] picFile,@RequestParam Integer count,@RequestParam String type,@RequestParam String service);
    5. }

    2.4、使用

    1. MockMultipartFile imageMultiFile1 = new MockMultipartFile(image1.getName(),image1.getName(),null,new FileInputStream(image1));
    2. MockMultipartFile imageMultiFile2 = new MockMultipartFile(image2.getName(),image2.getName(),null,new FileInputStream(image2));
    3. MockMultipartFile multiFile1 = new MockMultipartFile(file1.getName(),file1.getName(),null,new FileInputStream(file1));
    4. MockMultipartFile multiFile2 = new MockMultipartFile(file2.getName(),file2.getName(),null,new FileInputStream(file2));
    5. MockMultipartFile[] mockMultipartFiles = new MockMultipartFile[]{imageMultiFile1,imageMultiFile2,multiFile1,multiFile2};
    6. ResponseVO fileResponse = fileClient.upload(mockMultipartFiles,4,"file","upload");
    7. log.info("文件上传结果:{}",fileResponse);

    2.5、file接收文件服务:

    1. @RestController
    2. @RequestMapping(value = "/file")
    3. @Slf4j
    4. public class UploadFileController {
    5. @PostMapping(value = "/upload")
    6. public ResponseVO upload(HttpServletRequest request) {
    7. MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
    8. String type = multipartHttpServletRequest.getParameter("type");
    9. Integer count = multipartHttpServletRequest.getParameter("count");
    10. for(int i=1;i<=count;i++) {
    11. MultipartFile mf = multipartHttpServletRequest.getFile("file" + i);
    12. }
    13. }
    14. }

  • 相关阅读:
    郑州轻工业大学OJ合集(C语言)【正在整理】
    GitLab使用记录
    Spring过滤器和拦截器的区别
    【C++初阶】一、入门知识讲解(C++关键字、命名空间、C++输入&输出、缺省参数、函数重载)
    关于一次两段式提交和数据库恢复数据我的一些想法
    博云入选 Gartner 中国 DevOps 代表厂商
    C++——cv::Rect数据结构详解
    关于OWL-carousel插件在ajax调用后需要重新实例化问题(页面无轮播效果)
    jdk11使用反射创建实例对象
    (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • 原文地址:https://blog.csdn.net/u011649691/article/details/126172436