远程调⽤⽂件上传踩坑指南:status500readingFileUploadOpenFe。。。
远程调⽤⽂件上传踩坑指南
1. ⽣产端所报异常:
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: at.util.http.fileupload.Fi leUploadException: the request was rejected because no multipart boundary was found] with root cause
at.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
2. 消费端所报异常
status 500 reading FileUploadOpenFeignClient#upload(MultipartFile)
3. ⽣产者代码
  controller层代码
@RestController
@RequestMapping("/file")
@Api(value ="⽂件上传")
public class FileUploadController {
@Autowired
FileUploadService fileUploadService;
@PostMapping(value ="/upload")
public String upload(@RequestParam("file") MultipartFile file){
if(ObjectUtil.isNull(file)){
return"⽂件不存在";
}
System.out.Size());
//获取⽂件的名称
String fileName = OriginalFilename();
return fileUploadService.upload(file,fileName);
}
}
  service层代码
@Service
public class FileUploadServiceImpl implements FileUploadService {
resource和autowired注解的区别@Override
public String upload(MultipartFile multipartFile,String fileName){
System.out.println("⽂件");
try{
String suffix = Name(fileName);
//扩展名
System.out.println(suffix);
String newFileName = place(UUID.randomUUID()+"."+ suffix,"-","");            System.out.println(newFileName);
String fileUploadPath ="E://Image//"+newFileName;
System.out.println(fileUploadPath);
File file =new File(fileUploadPath);
return UrlMappingCommon.FILE_URL+newFileName;
}catch(Exception e){
e.printStackTrace();
return"error";
}
}
}
  ⽂件路径映射
public class UrlMappingCommon {
public final static String FILE_URL ="localhost:8081/files/";
public final static String FILE_MAP ="/files/**";
}
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(UrlMappingCommon.FILE_MAP)
.addResourceLocations("file:E://Image//");
}
}
⽣产端postman测试正常
4. 消费端代码(错误⽰例)
@FeignClient(value ="mytesy1")
public interface FileUploadOpenFeignClient {
@PostMapping(value ="/file/upload",
consumes ={MediaType.MULTIPART_FORM_DATA_VALUE} )
public String upload(@RequestParam("file") MultipartFile file);
}
@RestController
@RequestMapping("/consumer")
@Api(tags ="学⽣远程调⽤")
public class ConsumerController {
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file){
return feignClient.upload(file);
}
}
    postman测试端调⽤出现问题
4. 消费端代码(正确⽰例)
@FeignClient(value ="mytesy1")
public interface FileUploadOpenFeignClient {
@PostMapping(value ="/file/upload",
consumes ={MediaType.MULTIPART_FORM_DATA_VALUE}
)
public String upload(@RequestPart("file") MultipartFile file);
}
@RestController
@RequestMapping("/consumer")
@Api(tags ="学⽣远程调⽤")
public class ConsumerController {
@PostMapping("/upload")
public String upload(@RequestPart("file") MultipartFile file){
return feignClient.upload(file);
}
}
总结:
    1. 远程调⽤⽂件上传必须加@RequestPart注解才能正常调⽤,@RequestParam就会报500错误,具体博主也不知道为啥⼦(⽣产端不需要改,消费端需要改),有懂的⼤佬可以评论区交流⼀下。
    2. 写openFeignClient的时候在@postmapping后⾯加上
consumes ={MediaType.MULTIPART_FORM_DATA_VALUE

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。