项目场景:
使用springcloud +Openfeign+minio上传文件时报错
问题描述
其实时在上传文件时,请求头中的信息没有被feign支成功正确的扫描到。
报错
报错2024-06-04 09:39:16.826 ERROR 13708 --- [nio-8888-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55]
at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:205) ~[tomcat-embed-core-9.0.55.jar:9.0.55]
at
问题分析
服务发送方
@RestController
@RequestMapping("/product/oss")
public class FileController {
@Resource
private OpenFenginService openFenginService;
//上传文件
@PostMapping("/upload")
public String uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
String url = openFenginService.upload(uploadFile);
return url;
}
opfeignservice中代码
@FeignClient(value = "jingxi-oss")
public interface OpenFenginService {
@PostMapping(value = "/oss/upload")
public String upload(@RequestPart("uploadFile") MultipartFile uploadFile ) throws Exception;
@GetMapping("/oss/test")
public String test(@RequestParam String test);
}
服务接受方
package com.oss.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.oss.entity.FileInfo;
import com.oss.entity.ImgDto;
import com.oss.entity.Result;
import com.oss.service.FileService;
import com.oss.util.MinioUtil;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
//热更新
@RefreshScope
@RestController
@RequestMapping("/oss")
public class FileController {
@Resource
private FileService fileService;
@Resource
private MinioUtil minioUtil;
@NacosValue(value = "${storage.service.type}",autoRefreshed = true)
private String storageType;
@RequestMapping("/testGetAllBuckets")
public String testGetAllBuckets() throws Exception {
List<String> allBucket = fileService.getAllBucket();
return allBucket.get(0);
}
/**
* 上传文件
*/
@PostMapping("/upload")
public String upload(@RequestParam("uploadFile") MultipartFile uploadFile ) throws Exception {
String bucket ="demo";
String objectName="test";
String url = fileService.uploadFile(uploadFile, bucket, objectName);
return url;
}
@GetMapping("/test")
public String test(@RequestParam String test){
return test;
}
}
解决方案:
修改openfeign中方法:
修改的两点:
- 请求参数中注解加上consumes = MediaType.MULTIPART_FORM_DATA_VALUE
- @RequestParam修改成为@RequestPart
@FeignClient(value = "jingxi-oss")
public interface OpenFenginService {
@PostMapping(value = "/oss/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload(@RequestPart("uploadFile") MultipartFile uploadFile ) throws Exception;
@GetMapping("/oss/test")
public String test(@RequestParam String test);
}