Spring MVC - 简单文件上传HTTP状态500 - 无法解析多部分servlet请求

时间:2016-08-31 10:09:22

标签: java file maven spring-mvc upload

我开始学习Spring MVC。想要将Excel文件上传到数据库的Web应用程序。

我从简单的文件上传开始,并在开始时遇到问题。

我使用Maven和Tomcat 8在Netbans中创建的项目。

我用教程

制作了简单的示例项目

https://saarapakarinen.wordpress.com/2015/01/11/building-a-basic-spring-3-web-project-netbeans-and-maven/

它完美无缺,但我想根据官方的Spring帮助将其更改为文件上传

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart

我打开了文件上传表格,当我选择文件并提交时我收到了错误:

HTTP Status 500 - Could not parse multipart servlet request; nested exception is 
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

如何解决这个问题以及错误在哪里?这是我的代码中的错误,或者我错过了什么,或者配置错误了?

我在form.jsp文件和一个控制器类HelloController上有一个表单,为什么它不起作用?

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                               http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <!-- name of the project//-->
        <display-name>HelloProject</display-name>
        <servlet>
            <servlet-name>front-controller</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>


        <servlet-mapping>
            <servlet-name>front-controller</servlet-name>
            <url-pattern>/application/*</url-pattern>
        </servlet-mapping>

        <!-- max time of the session //-->

        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <!-- default page //-->
        <welcome-file-list>
            <welcome-file>application/form.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

前端控制器-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <!-- configuration to fetch jsp files automatically from WEB-INF/jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean>

    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="1000000"/>



</bean>


    <context:component-scan base-package="helloweb"/>



</beans>    

form.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Form Page</title>
    </head>
    <body>

            <form method="POST" enctype="multipart/form-data" action="">
              <label>file to send: <input type="file" name="file" /></label>
              <input type="submit" />
            </form>
    </body>
</html>

HelloController.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helloweb;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class HelloController 
{

    private String startmessage="start";

    @RequestMapping("form")
    public String viewLoginPage(Model model)
    {  
          model.addAttribute("message", startmessage);
        return "form";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String login(
            @RequestParam(value = "file", required = true) MultipartFile file) 
    {


    if((file.isEmpty()) )
    {
        startmessage="file empty";
        return "redirect:form";        
    }
    else
    {
    byte[] bytes = file.getBytes();
    return "hello";
    }

    }
}

0 个答案:

没有答案