编程语言
首页 > 编程语言> > java – 在Jetty下工作但不在Tomcat下工作的文件上传

java – 在Jetty下工作但不在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;
    }
}

而且它正在工作!!

谢谢大家的帮助.

标签:java,jsp,spring,tomcat,maven-jetty-plugin
来源: https://codeday.me/bug/20190629/1321758.html