Announcement Announcement Module
Collapse
No announcement yet.
Newbie: MultipartResolver Upload Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie: MultipartResolver Upload

    Hi, a newbie question: Any example of MultipartResolver? Or it could be: what did I do wrong?

    In summary, I followed Ch 12.8 of the reference, and the following things went wrong:

    - in my SimpleFormController, onSubmit was never called. None of the 3 onSubmit functions were called. Only processFormSubmission was called
    - in the function processFormSubmission, I can get the "simple" properties like String, but cannot see the file. It is NULL

    Here's what I did:

    (1) edit the servlet.xml:
    Code:
    	<bean id="fileUploadController" class="web.FileUploadController">
    		<property name="commandClass"><value>web.FileUploadBean</value></property>
    		<property name="formView"><value>uploadReport</value></property>
    		<property name="successView"><value>uploadConfirmation</value></property>
    	</bean>
    	
    	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<property name="maxUploadSize"><value>1000000000</value></property>
    	</bean>
    (2) created a simple Bean to test:
    Code:
    public class FileUploadBean &#123;
        private byte&#91;&#93; file;
        private String username; 
        
        public void setFile&#40;byte&#91;&#93; file&#41; &#123;
            this.file = file;
        &#125;
        
        public byte&#91;&#93; getFile&#40;&#41; &#123;
            return file;
        &#125;
        /**
         * @return Returns the username.
         */
        public String getUsername&#40;&#41; &#123;
            return username;
        &#125;
        /**
         * @param username The username to set.
         */
        public void setUsername&#40;String username&#41; &#123;
            this.username = username;
        &#125;
    &#125;
    (3) created FileUploadeController
    Code:
    public class FileUploadController extends SimpleFormController &#123;
        protected void initBinder&#40;HttpServletRequest request,
                ServletRequestDataBinder binder&#41; throws ServletException &#123;
            System.out.println&#40;"pre initBinder"&#41;;
            binder.registerCustomEditor&#40;byte&#91;&#93;.class, "file", new ByteArrayMultipartFileEditor&#40;&#41;&#41;;
            System.out.println&#40;"post initBinder"&#41;;
        &#125;
        
        protected boolean isFormSubmission&#40;HttpServletRequest arg0&#41; &#123;
            System.out.println&#40;"isFormSubmission&#58; "+arg0&#41;;
            // TODO Auto-generated method stub
            return super.isFormSubmission&#40;arg0&#41;;
        &#125;
    
        protected ModelAndView processFormSubmission&#40;HttpServletRequest arg0,
                HttpServletResponse arg1, Object arg2, BindException arg3&#41;
                throws Exception &#123;
            System.out.println&#40;"processFormSubmission"&#41;;
            System.out.println&#40;"request = "+arg0&#41;;
            System.out.println&#40;"response = "+arg1&#41;;
            System.out.println&#40;"object = "+arg2&#41;;
            System.out.println&#40;"exception = "+arg3&#41;;
            
            FileUploadBean fub = &#40;FileUploadBean&#41;arg2;
            System.out.println&#40;"fub = "+fub&#41;;
            System.out.println&#40;"fub username "+fub.getUsername&#40;&#41;&#41;;
            System.out.println&#40;"fub file "+fub.getFile&#40;&#41;&#41;;
            // TODO Auto-generated method stub
            return super.processFormSubmission&#40;arg0, arg1, arg2, arg3&#41;;
        &#125;
    
        protected ModelAndView showForm&#40;HttpServletRequest arg0,
                HttpServletResponse arg1, BindException arg2&#41; throws Exception &#123;
            System.out.println&#40;"showForm"&#41;;
            // TODO Auto-generated method stub
            return super.showForm&#40;arg0, arg1, arg2&#41;;
        &#125;
    
        protected void doSubmitAction&#40;Object arg0&#41; throws Exception &#123;
            System.out.println&#40;"doSubmitAction"&#41;;
            // TODO Auto-generated method stub
            super.doSubmitAction&#40;arg0&#41;;
        &#125;
    
        protected ModelAndView onSubmit&#40;Object arg0&#41; throws Exception &#123;
            System.out.println&#40;"onSubmit 1 arg"&#41;;
            // TODO Auto-generated method stub
            return super.onSubmit&#40;arg0&#41;;
        &#125;
    
        protected ModelAndView onSubmit&#40;Object arg0, BindException arg1&#41;
                throws Exception &#123;
            System.out.println&#40;"onSubmit 2 args"&#41;;
            // TODO Auto-generated method stub
            return super.onSubmit&#40;arg0, arg1&#41;;
        &#125;
    
        protected ModelAndView onSubmit&#40;HttpServletRequest arg0,
                HttpServletResponse arg1, Object arg2, BindException arg3&#41;
                throws Exception &#123;
            System.out.println&#40;"onSubmit 4 args"&#41;;
            // TODO Auto-generated method stub
            return super.onSubmit&#40;arg0, arg1, arg2, arg3&#41;;
        &#125;
    &#125;
    (4) create the uploadReport.jsp
    Code:
    <html>
    <body>
    	<h1>Upload a file</h1>
    	<form method="POST" action="upload.form" encrypt="multipart/form-data">
    		<input type="file" name="file"/>
    		<input name="username" value="Test User"/><br>
    		<input type="submit"/>
    	</form>
    </body>
    </html>

    Sorry if this seem a little entry-level, but I have spent hours googling and reading the javadoc and still have no idea where to go next. Any pointers will be greatly appreciated!

    Paul

  • #2
    Should that be
    Code:
    entype="multipart/form-data"

    Comment


    • #3
      Oh my god...

      Yep, that seems to be the problem! I can't believe it's a typo...

      Thanks!

      Comment


      • #4
        The requested resource (/springapp/upload.form) is not

        I also followed Ch 12.8 of the reference, and the following things went wrong:
        HTTP Status 404 - /springapp/upload.form

        --------------------------------------------------------------------------------

        type Status report

        message /springapp/upload.form

        description The requested resource (/springapp/upload.form) is not available.


        --------------------------------------------------------------------------------

        Apache Tomcat/5.0.27

        by the way,Code Should be:
        enctype="multipart/form-data"

        Comment

        Working...
        X