0

I am using following code for uploading file. it works fine but the problem is that in uploading process arbitrary on a file it got hanged (No idea what is the reason).

-May be file is too long.

-May be Connection is not working fine.

but it remains hanged and finally i have to terminate it manually. so if there is any thing wrong then how can i get recognize time out doesn't matter whatever the reason just skip that file with some error.

FTPClient client = new FTPClient();
FileInputStream fis = null;


try {
    client.connect("32.178.10.121");
    client.login("XXX", "XXX");

    //
    // Create an InputStream of the file to be uploaded
    //

    File f = new File("D:\\FileFolder");
    if (f.isDirectory())
    {
        File[] listFiles = f.listFiles();
        for (int i = 0; i < listFiles.length; i++)
        {
            String filename = listFiles[i].getName();
            fis = new FileInputStream(filename);
            client.storeFile(filename, fis);

        }
    }

    //
    // Store file to server
    //
    client.logout();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
RTA
  • 1,251
  • 2
  • 14
  • 33

3 Answers3

0

One option is you can do your file upload in a thread. And you can timeout this thread if it is taking long time.

You can use ExecutorService service for this purpose.

Check this link for how to implement ExecutorService: How to timeout a thread

Are you using apache commons FtpClient? if so you can try using getStatus() to to continuously poll the status and close in case of some error. Or you can set timeout using setDataTimeout(int timeout) method.

Community
  • 1
  • 1
vishnu viswanath
  • 3,794
  • 2
  • 36
  • 47
0

If you are using JDK 1.7 I would recommend trying to downgrade to 1.6. It seems FTP is buggy in java 7, especially if you are using JDK's libraries. I was doing a program and it just froze my computer completely and would upload on and on but downgrading worked for me. If it's a larger application make another project with JDK 1.6 set and try just the FTP part.

0

You might want to try client.setControlKeepAliveTimeout(300);

See (possible duplicate): Commons FTPClient hangs after uploading large a file

Community
  • 1
  • 1
Joe F
  • 4,174
  • 1
  • 14
  • 13