我有一个带有spring的Web应用程序,在其中我进行了一些文件上传。在eclipse下,使用Jetty(Maven插件)可以完美工作。但是,当我在Tomcat下部署应用程序时,却没有,并且出现以下异常:
org.springframework.web.bind.MissingServletRequestParameterException: Required org.springframework.web.multipart.MultipartFile parameter 'file' is not present org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:545) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:336) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:207) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:132) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
这是我的表格:
<form method="post" action="../admin/import.html" id="import" enctype="multipart/form-data"> <div id="importInmates" align="center"> <input type="file" name="file" id="file" data-dojo-type="dijit.form.Button" label="<fmt:message key='import.file' />" /> <button data-dojo-type="dijit.form.Button" id="importInmates" type="submit"> <fmt:message key="import.import" /> </button> </div> <input type="hidden" name="importType" value="inmates" /> </form>
这是拦截方法:
@RequestMapping(value = IMPORT_PAGE, method = RequestMethod.POST) public String recieveFile(@RequestParam("importType") String importType, @RequestParam("file") MultipartFile multipartFile, final HttpSession session) { if (multipartFile.getSize() < 0) { LOGGER.debug("No file has been uploaded"); return "redirect:.." + IMPORT_PAGE; } File file = new File("tmp"); try { multipartFile.transferTo(file); BufferedReader lec = new BufferedReader(new FileReader(file)); LOGGER.debug(lec.readLine()); lec.close(); } catch (Exception e) { LOGGER.error("An exception occured while reading " + importType + " file", e); } return "redirect:.." + IMPORT_PAGE; }
我添加了以下bean:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000000"></property> </bean>
即使我认为只有后者很重要,但applicationContext.xml和mvc-servlet.xml都可以。
任何帮助,将不胜感激。
谢谢。
感谢@Bart,我得以找到以下简单的解决方案:
在拦截方法中,使用@ModelAttribute而不是@RequestParam:
@RequestMapping(value = IMPORT_PAGE, method = RequestMethod.POST) public String recieveFile(@RequestParam("importType") String importType, @ModelAttribute("file") UploadedFile uploadedFile, final HttpSession session) { MultipartFile multipartFile = uploadedFile.getFile();
其中UploadedFile是以下类:
public class UploadedFile { private String type; private MultipartFile file; public String getType() { return type; } public void setType(String type) { this.type = type; } public void setFile(MultipartFile file) { this.file = file; } public MultipartFile getFile() { return file; } }
它正在工作!
感谢大家的帮助。