Category Archives: apache-commons-fileupload

How does a file which exceeds the disk item factory memory threshold placed in the repository?

I'm under the impression that if a file that was uploaded using the apache commons fileUpload API exceeds the memory threshold declared/set by the Disk Item Factory object, it will go into a repository that would store these files temporarily. My questions is how does it place these files into the repository?

This is the code where I'm testing the scenario

if (ServletFileUpload.isMultipartContent(request)) {

            DiskFileItemFactory diskFactory = new DiskFileItemFactory();

            ServletFileUpload uploadHandler = new ServletFileUpload(diskFactory);
            diskFactory.setRepository(new File("/home/test/bin"));
            diskFactory.setSizeThreshold(5120);

try {
                List<FileItem> fileList = uploadHandler.parseRequest(request);
               for (FileItem file : fileList) {
                    if (!file.isFormField()) {

                        String name = new File(file.getName()).getName();
                        file.write(new File(
                                request.getServletContext().getInitParameter("UploadPath") + File.separator + name));
                    }

                }

            } catch (FileUploadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I set the memory threshold to 5kb, and I tried uploading a 13kb file. My expected result, it will be placed in the /home/test/bin directory. But it was placed in the upload directory path that I created for successful uploads.

Setting timeout for File Upload using Struts2

I have a requirement in an application to upload any type of file. Struts2 file upload interceptor has been used but I couldn't find any straight reference in Struts documentation to set timeout for upload.

Maximum allowable upload time is 5 minutes. If uploading is taking more than 5 minutes then user will get a message that timeout is occurred and file can not be uploaded due to network issue. Later user will handover such files to application owners. http://struts.apache.org/docs/file-upload.html

Any possible way to do it using Struts configuration or such timeout handling should be handled outside struts.

Error in uploading file stream ended unexpectedly

I am getting the below error

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly

My code was running fine earlier. I switched to 1.4 version of compiler.

public String uploadFile(HttpServletRequest request, String nomChamp) throws IOException {
    String directoryForUpload = EtSaveDirectory.getSaveDirectory().trim();
    String csv = (String) request.getAttribute("nomVisibleDuFichier" + nomChamp);
    if (StringUtils.isEmpty(csv)) {
        return null;
    }
    // Cas client Unix
    int pos = csv.lastIndexOf("/");
    if (pos == -1) {
        // Cas client Windows
        pos = csv.lastIndexOf("\\");
    }
    String importedFile = csv.substring(pos + 1).toString();
    String uploadedFile = directoryForUpload.concat(File.separator).concat(importedFile).trim();

    String contentType = request.getHeader("Content-type");
    byte[] boundary = contentType.substring(contentType.indexOf("boundary=") + 9).getBytes();
    InputStream input = request.getInputStream();
    MultipartStream multi = new MultipartStream(input, boundary);
    File file = new File(uploadedFile);
    if (file.exists()) {
        file.delete();
    }
    OutputStream output = new FileOutputStream(file);
    multi.readBodyData(output);
    output.close();

    return uploadedFile;
}

It is not able to readBodyData

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly 11:09:33,993 ERROR [BFC2_IHM] d├ętail de la trace : at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:540) 11:09:33,993 ERROR [BFC2_IHM] at bfc2.ihm.javabeans.FileUploadService.uploadFile(FileUploadService.java:57) 11:09:33,994 ERROR [BFC2_IHM] at bfc2.ihm.controleur.wb.Bfc2CtlWb18Importer.executeCtl(Bfc2CtlWb18Importer.java:43) 11:09:33,994 ERROR [BFC2_IHM] at et.services.controleur.EtControleur.performTask(EtControleur.java:633) 11:09:33,994 ERROR [BFC2_IHM] at et.services.controleur.EtControleur.doPost(EtControleur.java:204) 11:09:33,994 ERROR [BFC2_IHM] at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 11:09:33,994 ERROR [BFC2_IHM] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 11:09:33,995 ERROR [BFC2_IHM] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 11:09:33,995 ERROR [BFC2_IHM] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 11:09:33,995 ERROR [BFC2_IHM] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 11:09:33,995 ERROR [BFC2_IHM] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 11:09:33,995 ERROR [BFC2_IHM] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 11:09:33,995 ERROR [BFC2_IHM] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235) 11:09:33,996 ERROR [BFC2_IHM] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 11:09:33,996 ERROR [BFC2_IHM] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190) 11:09:33,996 ERROR [BFC2_IHM] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525) 11:09:33,996 ERROR [BFC2_IHM] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) 11:09:33,996 ERROR [BFC2_IHM] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) 11:09:33,996 ERROR [BFC2_IHM] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) 11:09:33,997 ERROR [BFC2_IHM] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 11:09:33,997 ERROR [BFC2_IHM] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 11:09:33,997 ERROR [BFC2_IHM] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) 11:09:33,997 ERROR [BFC2_IHM] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 11:09:33,997 ERROR [BFC2_IHM] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) 11:09:33,997 ERROR [BFC2_IHM] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) 11:09:33,998 ERROR [BFC2_IHM] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) 11:09:33,998 ERROR [BFC2_IHM] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) 11:09:33,998 ERROR [BFC2_IHM] at java.lang.Thread.run(Thread.java:662)

Java FTP using Apache commons throws "IOException caught while copying"

I have made a JavaFX application which includes uploading large files (> 1GB) to a server. Every time I got the same error on same place. Any Suggestions what am I doing wrong here.

ftpclient.connect(server, port);
ftpclient.login(ftpuser, ftppass);
ftpclient.enterLocalPassiveMode();
ftpclient.setKeepAlive(true);
ftpclient.setControlKeepAliveTimeout(3000);

Task<Void> copyMnt = new Task<Void>() {
@Override
protected Void call(){
  try {                              
      new Thread(new FTPHandler(ftpclient, source , dest)).run();
  } catch (IOException ex) {
     Logger.getLogger(MyAppController.class.getName()).log(Level.SEVERE, null, ex); 
  }
 return null;
  }
};


new Thread(copyMnt).start();

Now on the FTPHandler Class

// The constructor will set the ftpclient, source and destinations.
@Override

public void run() {
    try {
        uploadDirectory(this.getClient(), this.getDest(), this.getSrc(), "");
    } catch (IOException ex) {
        Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void uploadDirectory(FTPClient ftpClient,
        String remoteDirPath, String localParentDir, String remoteParentDir)
        throws IOException {

    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
        for (File item : subFiles) {
            String remoteFilePath = remoteDirPath + "/" + remoteParentDir
                    + "/" + item.getName();
            if (remoteParentDir.equals("")) {
                remoteFilePath = remoteDirPath + "/" + item.getName();
            }

            if (item.isFile()) {
                // upload the file
                String localFilePath = item.getAbsolutePath();
                java.util.Date date= new java.util.Date();

                System.out.println(new Timestamp(date.getTime()) + "  : Uploading :: " + localFilePath + " to " + remoteFilePath);
                boolean uploaded = uploadSingleFile(ftpClient,
                        localFilePath, remoteFilePath);
                if (uploaded) {
                    System.out.println("Success : "
                            + remoteFilePath);
                } else {
                    System.out.println("Failed : "
                            + localFilePath);
                }
            } else {
                // create directory on the server
                boolean created = ftpClient.makeDirectory(remoteFilePath);
                if (created) {
                    System.out.println("CREATED the directory: "
                            + remoteFilePath);
                } else {
                    System.out.println("COULD NOT create the directory: "
                            + remoteFilePath);
                }

                // upload the sub directory
                String parent = remoteParentDir + "/" + item.getName();
                if (remoteParentDir.equals("")) {
                    parent = item.getName();
                }

                localParentDir = item.getAbsolutePath();
                uploadDirectory(ftpClient, remoteDirPath, localParentDir,
                        parent);
            }
        }
    }
}

Every time I am uploading the files (files are of different types like .iso, .dat etc), The first few files (upload sequence is like first few hundred files are smaller i.e less than few MBs then Last 10 files are more than 1 GB big)will be successfully uploaded (i.e all smaller files and 2 of the last 10 files) but when it starts uploading the third big file I get following exception.

SEVERE: null
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
    at org.apache.commons.net.io.Util.copyStream(Util.java:134)
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)