一、对于单个文件上传
- @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/file/upload")
- ResponseVO<UploadFileVO> upload(@RequestPart MultipartFile picFile,@RequestParam Integer count,@RequestParam String type,@RequestParam String service);
-
-
- File image1 = new File(imagePath1);
- MockMultipartFile imageMultiFile1 = new MockMultipartFile(image1.getName(),new FileInputStream(image1));
- 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方法,处理多文件
- import feign.RequestTemplate;
- import feign.codec.EncodeException;
- import feign.codec.Encoder;
- import feign.form.ContentType;
- import feign.form.MultipartFormContentProcessor;
- import feign.form.spring.SpringFormEncoder;
- import feign.form.spring.SpringManyMultipartFilesWriter;
- import feign.form.spring.SpringSingleMultipartFileWriter;
- import org.springframework.web.multipart.MultipartFile;
-
- import java.lang.reflect.Type;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @Author: nzy
- * @Description:
- * @Date:Create:in 2022/8/5 10:27
- * @Modified By:
- */
- public class SpringMultipartEncoder extends SpringFormEncoder {
-
- public SpringMultipartEncoder() {
- this(new Default());
- }
-
- public SpringMultipartEncoder(Encoder delegate) {
- super(delegate);
- MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
- processor.addWriter(new SpringSingleMultipartFileWriter());
- processor.addWriter(new SpringManyMultipartFilesWriter());
- }
-
- @Override
- public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
- if (bodyType != null && bodyType.equals(MultipartFile.class)) {
- MultipartFile file = (MultipartFile) object;
- if (file != null) {
- Map
data = Collections.singletonMap(file.getName(), object); - super.encode(data, MAP_STRING_WILDCARD, template);
- return;
- }
- } else if (bodyType != null && bodyType.equals(MultipartFile[].class)) {
- if(object!=null) {
- MultipartFile[] files = (MultipartFile[]) ((MultipartFile[]) object);
- Map
data = new HashMap<>(); - for(int i=1;i<=files.length;i++) {
- data.put("file"+i,files[i-1]);
- }
- super.encode(data, MAP_STRING_WILDCARD, template);
- return;
- }
- }
- super.encode(object, bodyType, template);
- }
- }
2.2、config配置文件
- import feign.codec.Encoder;
- import org.springframework.beans.factory.ObjectFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Configurable;
- import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
- import org.springframework.cloud.openfeign.support.SpringEncoder;
- import org.springframework.context.annotation.Bean;
-
- /**
- * @Author: nzy
- * @Description:
- * @Date:Create:in 2022/8/5 10:27
- * @Modified By:
- */
- @Configurable
- public class MultipartSupportConfig {
-
- @Autowired
- private ObjectFactory
messageConverters; -
- @Bean
- public Encoder feignFormEncoder() {
- return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
- }
- }
2.3、在feignclient上配置configuration 属性为自定义的Encoder,这个config仅影响设置的feign
- @FeignClient(value="${file.microServiceName}",fallback = FileClientFallback.class,contextId = "dormClient",configuration = SpringMultipartEncoder.class)
- public interface FileClient {
- @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/file/upload")
- ResponseVO
upload(@RequestPart MultipartFile[] picFile,@RequestParam Integer count,@RequestParam String type,@RequestParam String service); - }
2.4、使用
- MockMultipartFile imageMultiFile1 = new MockMultipartFile(image1.getName(),image1.getName(),null,new FileInputStream(image1));
- MockMultipartFile imageMultiFile2 = new MockMultipartFile(image2.getName(),image2.getName(),null,new FileInputStream(image2));
- MockMultipartFile multiFile1 = new MockMultipartFile(file1.getName(),file1.getName(),null,new FileInputStream(file1));
- MockMultipartFile multiFile2 = new MockMultipartFile(file2.getName(),file2.getName(),null,new FileInputStream(file2));
-
- MockMultipartFile[] mockMultipartFiles = new MockMultipartFile[]{imageMultiFile1,imageMultiFile2,multiFile1,multiFile2};
- ResponseVO
fileResponse = fileClient.upload(mockMultipartFiles,4,"file","upload"); - log.info("文件上传结果:{}",fileResponse);
2.5、file接收文件服务:
- @RestController
- @RequestMapping(value = "/file")
- @Slf4j
- public class UploadFileController {
- @PostMapping(value = "/upload")
- public ResponseVO
upload(HttpServletRequest request) { - MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
- String type = multipartHttpServletRequest.getParameter("type");
- Integer count = multipartHttpServletRequest.getParameter("count");
- for(int i=1;i<=count;i++) {
- MultipartFile mf = multipartHttpServletRequest.getFile("file" + i);
- }
- }
-
- }