Spring 6文件上传问题求助:MultipartFile持续为空
Spring 6文件上传问题排查与解决
问题描述
Spring 6移除了org.springframework.web.multipart.commons.CommonsMultipartResolver,改用StandardServletMultipartResolver后出现以下问题:
MultipartFile对象和通过MultipartHttpServletRequest获取的文件均为Null- 给
MultipartFile添加@RequestParam("photo")或@RequestPart("photo")注解后,请求处理方法完全未触发,无打印输出
相关代码
Thymeleaf表单代码
<form th:action="@{/uploadFile}" enctype="multipart/form-data" method="post"> <div> <label for="photo">Choose a file to upload:</label> <input type="file" id="photo" name="photo"> </div> <br> <div> <button type="submit">Upload</button> </div> </form>
Java处理方法及Resolver Bean代码
// 我也试过基于XML的配置,但没用 @Bean public StandardServletMultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public String testUpload(HttpServletRequest request, MultipartFile photo, HttpSession session) throws IOException { System.out.println("MultipartFile: " + photo); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("photo"); System.out.println("multipartRequest.getFile(\"photo\"): " + multipartFile); String fileName = photo.getOriginalFilename(); String hzName = fileName.substring(fileName.lastIndexOf(".")); fileName = UUID.randomUUID().toString() + hzName; ServletContext servletContext = session.getServletContext(); String photoPath = servletContext.getRealPath("photo"); File file = new File(photoPath); if (!file.exists()) { file.mkdir(); } String finalPath = photoPath + File.separator + fileName; photo.transferTo(new File(finalPath)); return "success"; }
解决步骤
配置Servlet容器Multipart参数
StandardServletMultipartResolver依赖Servlet原生支持,需在application.properties中添加配置:spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB修正控制器方法参数
使用@RequestParam("photo")绑定文件,避免强制转换HttpServletRequest,同时增加空文件判断:@PostMapping("/uploadFile") public String testUpload(@RequestParam("photo") MultipartFile photo, HttpSession session) throws IOException { if (photo.isEmpty()) { System.out.println("上传文件为空"); return "error"; } System.out.println("MultipartFile: " + photo); String fileName = photo.getOriginalFilename(); String hzName = fileName.substring(fileName.lastIndexOf(".")); fileName = UUID.randomUUID().toString() + hzName; ServletContext servletContext = session.getServletContext(); String photoPath = servletContext.getRealPath("photo"); File file = new File(photoPath); if (!file.exists()) { file.mkdirs(); // 优先用mkdirs创建多级目录 } String finalPath = photoPath + File.separator + fileName; photo.transferTo(new File(finalPath)); return "success"; }移除自定义MultipartResolver Bean
Spring Boot环境下,配置开启后会自动注册StandardServletMultipartResolver,手动定义的Bean可能引发冲突,建议删除自定义的multipartResolver()方法。验证请求合法性
通过浏览器开发者工具检查请求头Content-Type是否为multipart/form-data,同时确保表单name="photo"与控制器参数注解中的名称完全匹配(大小写敏感)。
内容的提问来源于stack exchange,提问作者Yu Su
相关产品推荐
相关产品推荐

