[英]File upload working under Jetty but not under Tomcat
我有一个带弹簧的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;
}
}
而且它正在工作!!
谢谢大家的帮助。
使用@RequestPart
而不是@RequestParam
。
从来源:
可用于将“multipart / form-data”请求的一部分与方法参数相关联的注释。 支持的方法参数类型包括{@link MultipartFile}和Spring的{@link MultipartResolver}抽象,{@code javax.servlet.http.Part}以及Servlet 3.0多部分请求,或者其他任何方法参数,内容部分是通过{@link HttpMessageConverter}传递的,考虑到请求部分的“Content-Type”标题。 这类似于@ {@Link RequestBody}根据非多部分常规请求的内容解析参数的行为。
请注意,@ {link RequestParam}注释也可用于将“multipart / form-data”请求的一部分与支持相同方法参数类型的方法参数相关联。 主要区别在于,当method参数不是String时,@ {@ link RequestParam}依赖于通过注册的{@link Converter}或{@link PropertyEditor}进行类型转换,而@ {@ link RequestPart}依赖于{@link HttpMessageConverter考虑了请求部分的“Content-Type”标题。 @ {@ link RequestParam}可能与名称 - 值表单字段一起使用,而@ {@ link RequestPart}可能与包含更复杂内容的部分(例如JSON,XML)一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.