文件上传在Jetty下工作,但不在Tomcat下工作 [英] File upload working under Jetty but not under Tomcat

查看:302
本文介绍了文件上传在Jetty下工作,但不在Tomcat下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带弹簧的Web应用程序,我在其中进行一些文件上传。在eclipse下,使用Jetty(maven插件),它完美无缺。但是当我在Tomcat下部署应用程序时,它没有,我得到以下异常:

I have a web application with spring in which I do some file upload. Under eclipse, using Jetty (the maven plugin) it works perfectly. But when I deploy the application under Tomcat it does not and I get the following Exception :

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:

I have added the following bean :

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="100000000"></property>
</bean>

在applicationContext.xml和mvc-servlet.xml中,即使我认为只有后者很重要。

both in applicationContext.xml and mvc-servlet.xml even if I think that only the latter is important.

任何帮助都将不胜感激。

Any help would be appreciated.

谢谢。

推荐答案

感谢@Bart我找到了以下简单的解决方案:

Thanks to @Bart I was able to find the following simple solution :

在截取方法中,使用@ ModelAttribute而不是@RequestParam:

In the intercepting method, use @ModelAttribute instead of @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是以下类:

Where UploadedFile is the following class :

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;
    }
}

它正在工作!!

感谢大家的帮助。

这篇关于文件上传在Jetty下工作,但不在Tomcat下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆