Announcement Announcement Module
Collapse
No announcement yet.
Simple file upload controller not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple file upload controller not working

    I have attempted to create a simple form controller to use for file uploading as described in the example in the Spring reference http://static.springframework.org/sp...#mvc-multipart but things are not working as advertised. Maybe someone can see what I'm doing wrong.

    I have a controller which looks like this:
    Code:
    package mycom.admin.controller;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    import mycom.admin.controller.form.PrefsFileUploadForm;
    
    
    public class PrefsFileUploadFormController extends SimpleFormController {
        
        public PrefsFileUploadFormController () {
            setCommandClass(PrefsFileUploadForm.class);
        }
        
        /**
         * @param command
         * @throws ServletException
         */
        protected void doSubmitAction(Object command) throws ServletException {
    
            // cast the command
            PrefsFileUploadForm form = (PrefsFileUploadForm)command;
            
            // make sure we have content
            byte[] fileContent = form.getFile();
            if (fileContent == null) {
                throw new ServletException(this.getClass() + ".onSubmit():  No file content");
            }
            
        }
        
        /* (non-Javadoc)
         * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
         */
        /**
         * Register a custom editor (in this case the ByteArrayMultipartEditor)
         * to actually be able to convert Multipart instance to byte[].
         *
         * @param request
         * @param binder
         * @throws Exception
         */
        protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
            binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        }
    
    }
    The command object looks like this:
    Code:
    package mycom.admin.controller.form;
    
    
    public class PrefsFileUploadForm {
    
        private byte[] file;
        
        public byte[] getFile() {
            return this.file;
        }
        
        public void setFile(byte[] file) {
            this.file = file;
        }
        
    }
    The application context configuration file has the following bean definitions:
    Code:
        <!--  Controller for uploading a preferences XML file -->
        <bean id="prefsFileUploadFormController"
              class="mycom.admin.controller.PrefsFileUploadFormController">
            <property name="formView"
                      value="prefsFileUpload" />
            <property name="successView"
                      value="confirmation" />
        </bean>
    
        <!-- HandlerMapping which maps URLs to Controller IDs -->
        <bean id="urlMapping"
              class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/uploadPrefsFile.htm">
                        prefsFileUploadFormController
                    </prop>
                </props>
            </property>
        </bean>
    
        <!-- ViewResolver which adds a prefix and suffix to logical View names specified
             in the ModelView objects returned by Controller.handleRequest() methods -->
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix"
    			      value="/WEB-INF/jsp/" />
            <property name="suffix"
                      value=".jsp" />
        </bean>
    The view JSP that I'm using (prefsFileUpload.jsp):
    Code:
    <html>
        <head>
            <title>Preferences XML File Upload</title>
        </head>
        <body>
            <h1>Select a new Preferences XML file</h1>
            <form method="post"
                  action="uploadPrefsFile.htm"
                  enctype="multipart/form-data">
                <input type="file"
                       name="file"/>
                <input type="submit"/>
            </form>
        </body>
    </html>

    So when I enter the URL http://localhost:8080/sparkadmin/uploadPrefsFile.htm I should be getting into the controller by virtue of the URL mapping, and the controller will render the JSP (its formView) as the view since the request is a GET. When the file is submitted the controller's doSubmitAction() executes and the file is uploaded. At least the above is supposed to happen, but unfortunately I am getting a 404 message back from Tomcat, "The requested resource (/sparkadmin/uploadPrefsFile.htm) is not available."

    Can anyone suggest where I might be going wrong? Thanks in advance for any help!


    --James
    Last edited by monocongo; Dec 8th, 2005, 10:26 AM. Reason: removal of my company information

  • #2
    I have fixed the problem, it appears to have been related to the dispatcher servlet's mapping in the web.xml. Before I was mapping /* to the dispatcher servlet, now I am mapping *.htm and *.xml to the dispatcher (in two separate mappings). Everything works as I'd hoped now.


    --James

    Comment

    Working...
    X