java - 文件上传在 Jetty 下工作,但在 Tomcat 下不工作

标签 java spring jsp tomcat maven-jetty-plugin

我有一个带有 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;
    }
}

它正在工作!!

感谢大家的帮助。

关于java - 文件上传在 Jetty 下工作,但在 Tomcat 下不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698562/

相关文章:

java - [Spring MVC]- 如何检索 map 中的所有表单参数?

java - 如何将值从jsp传递到java类

java - 可运行界面示例

java - 如何跳过某些http请求的重定向到https?如何在jboss web子系统中配置它?

java - 无法确定 : com. packt.cardatabase.domain.Owner 的类型,位于表 : car, 的列 : [org. hibernate.mapping.Column(owner)]

java - Spring MVC。方法参数字段的默认值

java - JPA (Hibernate) 上的多个双向映射问题

java - 用于访问特定选项卡内容(如果存在)的通用 xpath

java - 我想从下拉列表中传递值的表单中应该使用什么 Spring Form 支持对象?

forms - Struts2 标签仅以文本形式检索单个字段错误