commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geoffroy EMPAIN <>
Subject FTPClient, resume an interrupted upload?
Date Sun, 05 May 2013 07:47:17 GMT
Hello everyone,

I'm currently working on an App that uploads large PDFs to a server. The app works fine, but
everytime, since the PDFs are so large (25MB), the upload takes a while, and usually after
let's say 30 or 40 minutes, I get a "socketException : broken pipe". I believe that this is
due to a timeout or disconnect from the server (the server cuts the connection I guess), so
I moved the upload routine in a loop that has a try / catch. When an exception is thrown,
I try to reconnect. It works fine. The upload starts where it stopped and completes.

Problem? Well, since the upload is "broken" in two parts (or more if any other connection
loss occurs), the file uploaded is broken too! I mean that's definatley normal and I unerstand
why that happens, but what I'd like to know is how you can keep the upload on "hold" while
trying to reconnect. I just want to be able to do a complete upload even if there are reconnections.
I want my PDF to uploaded completely. Here is the code I have :

// In
inputStream = new FileInputStream(localFile); 

// For upload loop
byte[] bytesIn = new byte[4096];
int read = 0;

// Loop until job is not completed (will try to reconnect if any exception is thrown)
boolean completed = false;
while (!completed){

        // Out
        OutputStream outputStream = ftpClient.storeFileStream(remoteFile);

        // Transfer                    
        while ((read = != -1) {
            outputStream.write(bytesIn, 0, read);
            transfered += read;

        // Closing streams

        // Final information
        completed = ftpClient.completePendingCommand();
        if (completed) System.out.println("Done");
        else System.out.println("Failure.");
        completed = true;

    } // end try 
    catch (Exception e) {

        // Telling the user
        System.out.println("Trying to reconnect...");

        // Try to reconnect
        ftpClient.connect(server, port);
        success = ftpClient.login(user, pass);
        if (success) {
            System.out.println("Connection : OK");
        } else {
            System.out.println("Failed to connect");

        // Set passive mode and file type to binary

    } // end catch
} // end loop

Any help will be greatly apreciated! Thanks to the community!

Best regards;

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message