Announcement Announcement Module
Collapse
No announcement yet.
Unable to read InputStream using MultipartHttpServletRequest Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to read InputStream using MultipartHttpServletRequest

    Hi Spring Team,

    I'm trying to use another OpenSource project www.jexcelapi.org to read the contents on Excel that I would like to import into my system. In order to do this I must obtain the InputStream via the request object in the Controller that I have created to handle the process. Unfortunately I get an exception regarding reading the input file:

    "The input file was not found"

    whenever I try to upload the file.

    My onSubmit method is as follows:


    Code:
    public ModelAndView onSubmit(HttpServletRequest request,
                                     HttpServletResponse response, Object command,
                                     BindException errors)
        throws Exception {
            
        	FileUpload fileUpload = (FileUpload) command;
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
            
            try {
                // retrieve the file data
                ServletInputStream stream = multipartRequest.getInputStream();            
    
                Workbook workbook = Workbook.getWorkbook(stream);
    
                // read and commit the uploaded spreadsheet
                mgr.readUploadedIntellectaulProperty(workbook);      
                
                // close the stream
                stream.close();
            }
            // JXL Api failed
            catch(JXLException j){
                if(log.isDebugEnabled()){
                    log.debug("JXLException created: "+j.getMessage());
                }
              
            }
            // Stream could not be opened
            catch(IOException ioe){
                if(log.isDebugEnabled()){
                    log.debug("IOException created: "+ioe.getMessage());
                }
            }
    My questions are:
    1) Is the code that I have written correct for the SpringMVC to read a multipart file InputStream?
    2) Is there any configuration settings I need to configure within SpringMVC to make this happen
    3) Would you have any suggestions to an alternative if this is incorrect

    I have included the JSP as reference incase I have any settings incorrect within it.

    Code:
    <form method="post" id="uploadForm" action="<c:url value="/uploadFile.html"/>"
        enctype="multipart/form-data" onsubmit="return validateUploadForm(this)">
    <table class="detail">
        <tr>
            <th>
                <lips:label key="uploadForm.name" />
            </th>
            <td>
                <input type="text" name="name" id="name" size="40"/>
            </td>
        </tr>
        <tr>
            <th>
                <lips:label key="uploadForm.file"/>
            </th>
            <td>
                <input type="file" name="file" id="file" size="50"/>
            </td>
        </tr>
        <tr>
            <td></td>
            <td class="buttonBar">
                <input type="submit" name="upload" class="button" onclick="bCancel=false"
                    value="<fmt:message key="button.upload"/>" />
                <input type="submit" name="cancel" class="button" onclick="bCancel=true"
                    value="<fmt:message key="button.cancel"/>" />
            </td>
        </tr>
    </table>
    </form>

  • #2
    Do you have a resolver in the application context?

    Comment


    • #3
      This is a copy of the action-servlet.xml

      <bean id="fileUploadController"
      class="au.edu.tlf.lips.webapp.action.FileUploadCon troller">
      <property name="commandName">
      <value>uploadForm</value>
      </property>
      <property name="commandClass">
      <value>au.edu.tlf.lips.webapp.action.FileUpload</value>
      </property>
      <!-- Validator does not support command class [au.edu.tlf.lips.webapp.action.FileUpload] -->
      <!-- I wish I knew why! -->
      <!--property name="validator"><ref bean="beanValidator"/></property-->
      <property name="formView">
      <value>selectFile</value>
      </property>
      <property name="successView">
      <value>displayFile</value>
      </property>
      </bean>
      <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.C ommonsMultipartResolver" />

      Comment


      • #4
        Dear users,

        I would really aspirate some suggestions if possible regarding this issue. I have tried numerous combinations to try and solve this. I have now changed Excel framework to Apache POI HSSF but as yet and still unable to success read the InputStream to create the workbook via the SpringMVC. I believe I have what is required in place but yet I continually get NullPointerExceptions relation to the InputStream when I try to do anything with it.

        I have tried upload the file and writing it to my local machine then re-opening it in Excel and that would correctly, hence I am at a lost as to why the application is not capable of passing the InputStream to my services layer and manipulating it from there.

        Any help would really help me as I have now been stuck on this issues for a week and my deadline is starting to come under serious pressue.

        Thank you

        Code:
            public ModelAndView onSubmit(HttpServletRequest request,
                                         HttpServletResponse response, Object command,
                                         BindException errors)
            throws Exception {
                
            	FileUpload fileUpload = (FileUpload) command;
            	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
            	MultipartFile file = multipartRequest.getFile("file"); 
        
            	try { 
        
             	    byte[] data = file.getBytes(); 
                    InputStream stream = new ByteArrayInputStream(data);
                
                    hssfworkbook = new HSSFWorkbook(stream); 
        
                    stream.close();
                    
                    // read and commit the uploaded spreadsheet
                    mgr.readUploadedIntellectaulProperty(hssfworkbook);
                }
                // Stream could not be opened
                catch(IOException ioe){
                    if(log.isDebugEnabled()){
                        log.debug("IOException created: "+ioe.getMessage());
                        ioe.printStackTrace();
                    }
                    errors.rejectValue("file",
        					"File could not be read",
        					new Object[] { fileUpload.getName() },
        					"File could not be read");
                    
                    return showForm(request, response, errors);
                }                
        
                return new ModelAndView(getSuccessView());    
        }

        The error is as follows:

        [java] [lips] DEBUG [http8080-Processor23] BaseCommandController.createCommand(267) | Creating new command of class [au.edu.tlf.lips.webapp.action.FileUpload]
        [java] [lips] DEBUG [http8080-Processor23] SimpleFormController.processFormSubmission(222) | No errors -> processing submit
        [java] [lips] ERROR [http8080-Processor23] FrameworkServlet.service(342) | Could not complete request
        [java] java.lang.NullPointerException
        [java] at au.edu.tlf.lips.webapp.action.FileUploadController .onSubmit(FileUploadController.java:85)
        [java] at org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:223)
        [java] at au.edu.tlf.lips.webapp.action.FileUploadController .processFormSubmission(FileUploadController.java:5 5)
        [java] at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:237)
        [java] at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:121)
        [java] at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
        [java] at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:495)
        [java] at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:321)
        [java] at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:284)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :73)
        [java] at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:73)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at com.opensymphony.clickstream.ClickstreamFilter.doF ilter(ClickstreamFilter.java:42)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at au.edu.tlf.lips.webapp.filter.ActionFilter.doFilte r(ActionFilter.java:160)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at au.edu.tlf.lips.webapp.filter.BreadcrumbFilter.doF ilter(BreadcrumbFilter.java:185)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at org.springframework.orm.hibernate.support.OpenSess ionInViewFilter.doFilterInternal(OpenSessionInView Filter.java:170)
        [java] at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:73)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at org.displaytag.filter.ResponseOverrideFilter.doFil ter(ResponseOverrideFilter.java:88)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at au.edu.tlf.lips.webapp.filter.GZIPFilter.doFilter( GZIPFilter.java:51)
        [java] at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:233)
        [java] at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:204)
        [java] at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:257)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
        [java] at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:567)
        [java] at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:245)
        [java] at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:199)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
        [java] at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:587)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:149)
        [java] at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:567)
        [java] at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:184)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
        [java] at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:164)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:149)
        [java] at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:567)
        [java] at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:156)
        [java] at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:151)
        [java] at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:567)
        [java] at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:972)
        [java] at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:206)
        [java] at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:833)
        [java] at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:732)
        [java] at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:619)
        [java] at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:688)
        [java] at java.lang.Thread.run(Unknown Source)

        Comment


        • #5
          Hello users,

          I still have this problem without any solution.

          Can anyone suggest possible issues, fixes for me?

          :roll:

          Comment

          Working...
          X