MultipartFile在spring mvc中不起作用

时间:2017-05-23 11:30:17

标签: jsp spring-mvc

当我使用 @RequestParam(" filename")MultipartFile文件

时,以下代码不会打印任何内容

但是当我使用 @RequestParam("名称")字符串名称

时,它会打印输出

这是我的控制器

@Controller
public class DocumentController {

@RequestMapping(value="/document", method=RequestMethod.POST)

      // second parameter doesn't work

public @ResponseBody String saveDocument(@ModelAttribute("document") 
     DocumentBean formdata,
    @RequestParam("filename") MultipartFile file)
     {
        System.out.println("hello");
        System.out.println(formdata.getDescription());

        // below code doesn't work

       System.out.println(file.getOriginalFilename());
       return null;

}

}

以下是jsp代码

  <body>
     <form action="#" id="documentForm" name="document" method="post" 
     enctype="multipart/form-data">
       <div>Name  <input type="Text" placeholder="enter name of file" 
       name="Name"></div>
       <div>Description  <input type="Text" placeholder="description" 
       name="description"></div>
       <div>Document <input type="file" name="filename" id="file"></div>
    </form>
    <button onclick="SubmitForm()">submit</button>  
 </body>

下面是web.xml

 <?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   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_3_0.xsd" id="WebApp_ID" 
   version="3.0">
    <display-name>DocumentProject</display-name>
     <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

     </welcome-file-list>

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

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

      <listener>
        <listener-class>
         org.springframework.web.context.ContextLoaderListener
       </listener-class>
      </listener>


      <context-param>
        <param-name>ContextConfigLocation</param-name>
        <param-value>/WEB-INF/applicaitonContext</param-value>
      </context-param>

      </web-app>

下面是dispatcher-servlet.xml

   <?xml version="1.0" encoding="UTF-8" ?>
   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:aop="http://www.springframework.org/schema/aop" 
   xmlns:tx="http://www.springframework.org/schema/tx"
   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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<mvc:annotation-driven />

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

<context:component-scan base-package="com.master.controller" />

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

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages</value>
        </list>
    </property>
</bean>

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="100000000" />
</bean>

<tx:annotation-driven transaction-manager="txManager" />
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
</bean>

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

下面是applicationContext.xml

 <?xml version="1.0" encoding="UTF-8" ?>
 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">



<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>

            // pojo list


        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
    </property>
</bean>

以下是SubmitForm功能

 <script>
  function SubmitForm(){

    var formdata=$("#documentForm").serialize();
    $.ajax({
     type:"Post",
     url:"document",
     data:formdata,
     success:function(res){

     },
     error:function(){

     }
   });
}
</script>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码段。

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page isELIgnored="false" %>
<form:form method="POST" commandName="fileUploadCmd" enctype="multipart/form-data">
Select File: <input type="file" name="uploadFile"/> <br>
<input type="submit" value="upload"/> 
</form:form>
相关问题