1 Reply Latest reply on Oct 12, 2012 2:54 AM by Devsachin

    Flex file upload issue with large image files

    Devsachin

          

           Hello, I have created a sample flex application to upload an image and also created java servlet to upload and save image and deployed in local tomcat server. I am testing the application in LAN. I am able to upload small as well as large image file(1Mb) from some PCs but in some other PCs I am getting IOError while uploading large image files however it is working fine for small images. Image uploading is hanging after 10%-20% and throwing IOError. *Surprizgly it is working Ok with XP systems and causeing issues with Windows7 systems*.
       
      Plz give me any idea to get a solution.

      In Tomcat server side it is giving following error:
      request: org.apache.catalina.connector.RequestFacade@c19694
      org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
              at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:371)
              at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.ja va:126)
              at flex.servlets.UploadImage.doPost(UploadImage.java:47)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j ava:290)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
              at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
              at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProto col.java:594)
              at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
              at java.lang.Thread.run(Thread.java:722)
      Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
              at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStre am.java:982)
              at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:8 86)
              at java.io.InputStream.read(InputStream.java:101)
              at org.apache.commons.fileupload.util.Streams.copy(Streams.java:96)
              at org.apache.commons.fileupload.util.Streams.copy(Streams.java:66)
              at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:366)

      UploadImage.java:


      package flex.servlets;

      import java.io.*;
      import java.sql.*;
      import java.util.*;
      import java.text.*;
      import java.util.regex.*;
      import org.apache.commons.fileupload.servlet.ServletFileUpload;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;
      import org.apache.commons.fileupload.*;

      import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;


      import javax.servlet.*;
      import javax.servlet.http.*;

      public class UploadImage extends HttpServlet{
              /**
               * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
               *      response)
               */
              protected void doGet(HttpServletRequest request,
                              HttpServletResponse response) throws ServletException, IOException {
                      // TODO Auto-generated method stub
                      doPost(request, response);
              }
             
              public void doPost(HttpServletRequest request,
                              HttpServletResponse response)
             
             
              throws ServletException, IOException {
                      PrintWriter out = response.getWriter();
                      boolean isMultipart = ServletFileUpload.isMultipartContent(
                                      request);
                      System.out.println("request: "+request);
                      if (!isMultipart) {
                              System.out.println("File Not Uploaded");
                      } else {
                              FileItemFactory factory = new DiskFileItemFactory();
                              ServletFileUpload upload = new ServletFileUpload(factory);
                              List items = null;

                              try {
                                      items = upload.parseRequest(request);
                                      System.out.println("items: "+items);
                              } catch (FileUploadException e) {
                                      e.printStackTrace();
                              }
                              Iterator itr = items.iterator();
                              while (itr.hasNext()) {
                                      FileItem item = (FileItem) itr.next();
                                      if (item.isFormField()){
                                              String name = item.getFieldName();
                                              System.out.println("name: "+name);
                                              String value = item.getString();
                                              System.out.println("value: "+value);
                                      } else {
                                              try {
                                                      String itemName = item.getName();
                                                      Random generator = new Random();
                                                      int r = Math.abs(generator.nextInt());

                                                      String reg = "[.*]";
                                                      String replacingtext = "";
                                                      System.out.println("Text before replacing is:-" +
                                                                      itemName);
                                                      Pattern pattern = Pattern.compile(reg);
                                                      Matcher matcher = pattern.matcher(itemName);
                                                      StringBuffer buffer = new StringBuffer();

                                                      while (matcher.find()) {
                                                              matcher.appendReplacement(buffer, replacingtext);
                                                      }
                                                      int IndexOf = itemName.indexOf(".");
                                                      String domainName = itemName.substring(IndexOf);
                                                      System.out.println("domainName: "+domainName);

                                                      String finalimage = buffer.toString()+"_"+r+domainName;
                                                      System.out.println("Final Image==="+finalimage);

                                                      File savedFile = new File(getServletContext().getRealPath("assets/images/")+"/LowesFloorPlan.png");
                                                      //File savedFile = new File("D:/apache-tomcat-6.0.35/webapps/ROOT/example/"+"\\test.jpeg");
                                                      item.write(savedFile);
                                                      out.println("<html>");
                                                      out.println("<body>");
                                                      out.println("<table><tr><td>");
                                                      out.println("");
                                                      out.println("</td></tr></table>");
                                                      try {
                                                              out.println("image inserted successfully");
                                                              out.println("</body>");
                                                              out.println("</html>");  
                                                      } catch (Exception e) {
                                                              System.out.println(e.getMessage());
                                                      } finally {
                                                             
                                                      }  
                                              } catch (Exception e) {
                                                      e.printStackTrace();
                                              }
                                      }
                              }
                      }
              }
      }