2015-02-10 126 views
2

我有一個更新文件形式的Spring應用程序:(Spring MVC的)上傳multipartfile POST方法將返回404錯誤

<form class="form-inline" role="form" method="POST" enctype="multipart/form-data" 
        action="<c:url value="upload" />"> 
       <div class="form-group"> 
        <label class="control-label"> <spring:message code="file.to.upload"/> </label> 
       </div> 
       <div class="form-group"> 
        <input type="file" name="file" id="file" class="btn btn-primary"> 
       </div> 
       <div class="form-group"> 
        <input 
          type="submit" 
          class="btn btn-primary" 
          value=<spring:message code="upload"/>> 

       </div> 
      </form> 

但是,當我提交表單,我得到一個404錯誤,介紹稱資源()不可用。請注意,它不會說「(/ upload)」,只是一個空()。

我的(沒有達到)控制器:

@Controller 
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) 
public class WebController implements Serializable { 
... 

@RequestMapping(value="upload", method=RequestMethod.POST) 
public String handleFileUpload(
     @RequestParam("file") MultipartFile file, ModelMap model) throws  CloneNotSupportedException { 
    System.out.println("Handling file"); 
    ... //never reaches here 
    return screen(model); 
} 

而且我有這個在我的MVC-調度-servlet.xml中:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/pages/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<jpa:repositories base-package="package"/> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager"/> 
<mvc:annotation-driven/> 
<!-- Root Context: defines shared resources visible to all other web components --> 
<bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize"> 
     <value>104857600</value> 
    </property> 
    <property name="maxInMemorySize"> 
     <value>4096</value> 
    </property> 
</bean> 

<bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:basename="resources/locale/strings"> 
    <property name="defaultEncoding" value="UTF-8"/> 
</bean> 

<bean id="localeChangeInterceptor" 
     class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
    <property name="paramName" value="locale"/> 
</bean> 

<bean id="localeResolver" 
     class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
    <property name="defaultLocale" value="es"/> 
    <property name="cookieName" value="myAppLocaleCookie"></property> 
    <property name="cookieMaxAge" value="3600"></property> 
</bean> 

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" > 
    <property name="interceptors"> 
     <list> 
      <ref bean="localeChangeInterceptor" /> 
     </list> 
    </property> 
</bean> 

<mvc:interceptors> 
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="locale"></property> 
    </bean> 
</mvc:interceptors> 

<mvc:resources mapping="/resources/**" location="/resources/"/> 

<!-- EntityManagerFactory --> 
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="persistenceUnit"/> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 


<!-- Enable @Transactional annotation --> 
<tx:annotation-driven/> 

和web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 

<!-- Spring Security Configuration File --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-security.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlet and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
</listener> 

<!-- Spring Security Filter --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 


<!--End security--> 

<!-- Spring MVC - START --> 
<filter> 
    <filter-name>encoding-filter</filter-name> 
    <filter-class> 
     org.springframework.web.filter.CharacterEncodingFilter 
    </filter-class> 
    <init-param> 
     <param-name>encoding</param-name> 
     <param-value>UTF-8</param-value> 
    </init-param> 
    <init-param> 
     <param-name>forceEncoding</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>encoding-filter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<welcome-file-list> 
    <welcome-file>/</welcome-file> 
</welcome-file-list> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.css</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.js</url-pattern> 
</servlet-mapping> 

<session-config> 
    <session-timeout>60</session-timeout> 
</session-config> 

在Maven POM文件:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.springapp</groupId> 
<artifactId>Project</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>Project</name> 

<properties> 
    <jdk.version>1.6</jdk.version> 
    <spring.version>3.2.8.RELEASE</spring.version> 
    <spring.security.version>3.2.3.RELEASE</spring.security.version> 
    <jstl.version>1.2</jstl.version> 
    <mysql.connector.version>5.1.30</mysql.connector.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>2.1</version> 
     <scope>provided</scope> 
    </dependency> 


    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>${spring.version}</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
    </dependency> 


    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-jpa</artifactId> 
     <version>1.2.0.RELEASE</version> 
    </dependency> 

    <!-- Hibernate --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.3.3.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.3.3.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>2.2.9</version> 
    </dependency> 

    <dependency> 
     <groupId>org.json</groupId> 
     <artifactId>json</artifactId> 
     <version>20080701</version> 
    </dependency> 

    <!-- Everything Else for database--> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>${mysql.connector.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 

    <dependency> 
     <groupId>xerces</groupId> 
     <artifactId>xercesImpl</artifactId> 
     <version>2.11.0</version> 
    </dependency> 

    <!-- Spring Security --> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-taglibs</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-fileupload</groupId> 
     <artifactId>commons-fileupload</artifactId> 
     <version>1.3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-io</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>2.4</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.3.2</version> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-core-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-math3</artifactId> 
     <version>3.0</version> 
    </dependency> 


    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi</artifactId> 
     <version>3.10-FINAL</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi-ooxml</artifactId> 
     <version>3.10-FINAL</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi-ooxml-schemas</artifactId> 
     <version>3.10-FINAL</version> 
    </dependency> 

    <dependency> 
     <groupId>joda-time</groupId> 
     <artifactId>joda-time</artifactId> 
     <version>2.3</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-lang3</artifactId> 
     <version>3.3.2</version> 
    </dependency> 

    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.3.1</version> 
    </dependency> 

</dependencies> 

<build> 
    <finalName>Pronject</finalName> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
       <includes> 
        <include>**/*Tests.java</include> 
       </includes> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

感謝

+0

你可以嘗試映射「/上傳」你requestMapping? – minion 2015-02-10 14:53:31

+0

如果你的意思是'@RequestMapping(value =「/ upload」,method = RequestMethod.POST)',它不起作用,在這裏也是一樣的結果。注意:其他GET映射工作正常。 – detoro84 2015-02-10 15:00:30

+0

你可以發佈確切的堆棧跟蹤或錯誤的屏幕截圖嗎? – minion 2015-02-10 15:04:22

回答

2

的問題是因爲我使用Spring Security的使用CSRF保護。

this Spring doc有解決方案。 最後我解決它,包括行動CSRF令牌如下:

<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data"> 
+0

謝謝!這解決了我的問題.. – 2016-03-08 03:30:25