title: Read a file uploaded with Struts
body: <p>I try to read a file which is uploaded through browsing. For that i am using apache commons fileupload jar. So before run it with the project application i simply do a servlet/jsp POC project which successfully reads the file. Below is the code,</p>
  
  <pre><code>protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          boolean isMultipart = FileUpload.isMultipartContent(request);
          String content = "";
  
          try {
              if (isMultipart) {
                  FileItemFactory factory = new DefaultFileItemFactory();
                  FileUploadBase upload = new FileUpload(factory);
                  String[] arr;
                  List items;
  
                  items = upload.parseRequest(request);
  
                  Iterator iter = items.iterator();
                  while (iter.hasNext()) {
                      FileItem item = (FileItem) iter.next();
                      if (!item.isFormField()) {
                          BufferedInputStream buff = new    BufferedInputStream(
                                  item.getInputStream());
                          byte[] bytes = new byte[buff.available()];
                          buff.read(bytes, 0, bytes.length);
                          content = new String(bytes);
                          arr = content.split(",");
  
                      }
                  }
              }
  
              request.getRequestDispatcher("success.jsp").forward(request,
                      response);
          } catch (FileUploadException e) {
  
          }
  </code></pre>
  
  <p>I have use the jar version 1.1 becuase my project directory has that version of the jar and it use java 1.4. With java 1.5 we suppose to use ServletFileUpload instead of FileUpload . But i guess that does not matter where i can do the work with ealier version of jar.</p>
  
  <p>The problem comes when i use it with the real application which use struts 1.1. All seems fine but i dont return items when parsing the request.</p>
  
  <p>items = upload.parseRequest(request);</p>
  
  <p><strong>items is empty here.</strong></p>
  
  <p>So the difference i can find is that POC project which successfull show result is a JSP/Servlet project where the real application is using Struts.</p>
  
  <p>Is the request going to be differe in JSP/Servlet and Struts project in this case?</p>
  
  <p>In both case i use below in the form.</p>
  
  <pre><code>&lt;form action="SampleWeb" method="post" enctype="multipart/form-data"&gt;
  &lt;input type="file" name="datafile" size="40"&gt;
  &lt;input type="submit" value="Submit"&gt;
  </code></pre>
  
  <p>Below is the only addition in the struts application,</p>
  
  <pre><code>    &lt;action
          path="/ReportsAction"
          type="come.test.ReportsAction"
          name="ReportsForm"
          scope="request"
          validate="false"
          parameter="dispatch"&gt;
          &lt;forward name="success" path="/reports/A.jsp"/&gt;
          &lt;forward name="updatesuccess" path="/common/B.jsp"/&gt;              
      &lt;/action&gt;
  </code></pre>