Announcement Announcement Module
No announcement yet.
Simple file upload controller not working Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 but things are not working as advertised. Maybe someone can see what I'm doing wrong.

    I have a controller which looks like this:
    package mycom.admin.controller;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    import mycom.admin.controller.form.PrefsFileUploadForm;
    public class PrefsFileUploadFormController extends SimpleFormController {
        public PrefsFileUploadFormController () {
         * @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:
    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:
        <!--  Controller for uploading a preferences XML file -->
        <bean id="prefsFileUploadFormController"
            <property name="formView"
                      value="prefsFileUpload" />
            <property name="successView"
                      value="confirmation" />
        <!-- HandlerMapping which maps URLs to Controller IDs -->
        <bean id="urlMapping"
            <property name="mappings">
                    <prop key="/uploadPrefsFile.htm">
        <!-- ViewResolver which adds a prefix and suffix to logical View names specified
             in the ModelView objects returned by Controller.handleRequest() methods -->
        <bean id="viewResolver"
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix"
    			      value="/WEB-INF/jsp/" />
            <property name="suffix"
                      value=".jsp" />
    The view JSP that I'm using (prefsFileUpload.jsp):
            <title>Preferences XML File Upload</title>
            <h1>Select a new Preferences XML file</h1>
            <form method="post"
                <input type="file"
                <input type="submit"/>

    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!

    Last edited by monocongo; Dec 8th, 2005, 11: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.