springmvc上传文件,没有MultipartFile参数名报错直接 500,The current request is not a multipart急
各位大神求助,刚开始使用springmvc不是很了解,
展示controller 代码
@RequestMapping(value = "/uploadTest",method={POST,GET})
@ResponseBody
public String uploadTest(@RequestParam(required=false,value="file")MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IOException{
if(file == null){
return "没有图片文件";
}
String fileName = file.getOriginalFilename();
String filePath = request.getSession().getServletContext().getRealPath("/")+"/upload/";
File file2 = new File(filePath, System.currentTimeMillis() + fileName);
if(!file2.getParentFile().exists()){
file2.getParentFile().mkdir();
}
if(!file2.exists()){
file2.createNewFile();
}
file.transferTo(file2);
String name = file2.getName();
return null;
}
前端请求时 有file字段名是不会报错了,但没有文件或图片字段就直接 500 了,不管怎么调试都解决不了啊,网上也搜了不少解决方案但都是不行的
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMultipartRequest(RequestParamMethodArgumentResolver.java:216)
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:167)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:89)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:747)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:676)