1

Following code does not print anything when I use @RequestParam("filename")MultipartFile file

But it print output when I use @RequestParam("Name") String name

Here is my Controller

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

}

}

Below is jsp code

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

Below is 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>

Below is 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>

Below is 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>

Below is SubmitForm function

 <script>
  function SubmitForm(){

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

     },
     error:function(){

     }
   });
}
</script>
Aamir
  • 143
  • 10
  • 1
    You should add the JS code for `SubmitForm` –  May 23 '17 at 11:37
  • can you post your spring configuration file? – Akshay Khopkar May 23 '17 at 12:44
  • @RC, I have already added the JS code.Everything is working fine except multipart – Aamir May 24 '17 at 07:15
  • @AkshayKhopkar, check the configuration file – Aamir May 24 '17 at 07:26
  • ?!? no you did not, somewhere you have a javascript function named "SubmitForm" used here: ` –  May 24 '17 at 07:53
  • @RC. Check the SubmitForm() – Aamir May 24 '17 at 09:18
  • see https://stackoverflow.com/a/5976031/180100 for a possible solution –  May 24 '17 at 09:39
  • @RC. Thanks post cleared the doubt.Now its working. – Aamir May 25 '17 at 14:25
  • Possible duplicate of [Sending multipart/formdata with jQuery.ajax](https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax) –  May 25 '17 at 17:02
  • @Aamir Good news :) Could you please accept the duplicate proposal then? see https://meta.stackexchange.com/questions/250922/can-we-clarify-to-the-op-that-their-question-is-not-yet-closed-and-the-duplicate/250930#250930 –  May 25 '17 at 17:03

1 Answers1

0

You can try this snippet.

<%@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>
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23