Apache Commons FTPClient.storeFile() error Caused by: java.net.SocketException: Connection reset

I have made a new edit (2) and it seems the issue is that the images are in fact being stored, but sometimes it takes awhile to store them (many minutes, where normally it takes about a second). I'm curious if maybe it's my server? I found it odd I had no issues when I ran this (many months ago as I have been working on another project), so I'm curious why now there are issues, so server-side could make sense, but that's a big problem now if so...


I have an application that connects to my server and saves files/images to the server. I had originally saved the files on my computer, and then uploaded those files; however, since I am not looking to save files, I went the route of using a ByteArrayOutputStream.

For some reason, randomly during my file upload my JavaFX application will hang.

When clicking x it asks if I want to try and "restore" the application, and when I do it throws a stack trace with error.

Caused by: java.net.SocketException: Connection reset

I have searched multiple posts on here and Google, and nothing has helped me.

So far I've tried.

  1. "ftp.enterLocalPassiveMode(which is already enabled).
  2. Changing timeouts, such ass Connect and Data.
  3. Some bug that was in Java 7 which you have to run a commandline script in order to fix (I'm running Java 8 Update 71).
  4. some exec commands which seemed to only be useful for FTPSClient, not FTPClient

There was also something telling me to increase the buffersize and the user did "setBufferSize(1024*1024), while default, for me, was 0... Still didnt' change anything.

So far nothing is working, and the weird thing is that it will upload a random number of images, and then hang. I've been noticing that the more I try to upload these images, the more of them seem to upload, before hanging, but there was once it hung right on the first image, so I don't know...

I ran the Netbeans Profiler and I'm not sure if anything out of the oridinary was shown in the "Thread Dump", but I could post that if need be.


my code if it matters.

   for(Data f: plan.getData())
        {

                    BufferedImage b  = image;  //reduced code here
                    os =  new ByteArrayOutputStream();

                    ImageIO.write(b, "PNG", os);
                    is = new ByteArrayInputStream(os.toByteArray());


                    ftp.storeFile(f.getName(), is); // Application hangs/ Connection Resets here after x amount of stores.

       }

FTP Connection:

    {
         ftp.setDefaultTimeout(100000000);
      ftp.connect();
      ftp.login();

      // use local passive mode to pass firewall
      ftp.enterLocalPassiveMode();

        //System.out.print(ftp.getReplyString()); 


        //keeps connection alive (doesn't really work)

//Tried to set as many timeouts as I could as I read some of these could be the issue too...
        ftp.setKeepAlive(true);
         ftp.setControlKeepAliveTimeout(100000);
         ftp.setControlKeepAliveReplyTimeout(10000);
         ftp.setConnectTimeout(100000);
         ftp.setDataTimeout(300);
         ftp.setSoTimeout(100000);



        //sets buffer size to allow more files to be transferred.
        System.out.println("Buffer Size:" + ftp.getBufferSize());
        ftp.setBufferSize(1024 * 1024);
        System.out.println("Buffer Size:" + ftp.getBufferSize()); 


    }

Also, for what it's worth, after closing the program I get in my console, in red letters, the line

Java Result: -805306369

I cannot find this result number on Google, nor do I know if there is a specific place we look for results.

If anyone has any advice I would appreciate it...

This is really bothering me, especially since I had no problem at all storing files with images before....

Any help is appreciated thank you!


Stack Trace:

    SEVERE: null
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.read(BufferedReader.java:182)
    at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
    at org.apache.commons.net.ftp.FTP.getReply(FTP.java:694)
    at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1830)
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:689)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1996)

EDIT: I tried to do a change to just reading files, which is what I did the first time, and I get the same error.

I changed my code up a bit, and put it in different places, but everything is essentially the same...

The ONLY Change between these 2 versions, besides a lot of time, is that I switched computers... This is a JavaFX application, so I don't think the Changes I did to Tomcat on my old computer would matter, but I'm going to see if my old computer causes this issue....


EDIT2: I checked the old computer, and it's using the same version of Apache-Commons-Net (3.4) but I couldn't run the application in it's previous state, so I will see if I can fix it up to do so, or try other options.

For what it' s worth I decided to not click the "x" to exit, or touch the application at all, and just watched the outputs.

Normally, it would store a few files to the server, and then crash, but then I realized that my application "hangs" the entire time, and if I actually wait for awhile, it will store other images/files.

So now I'm confused exactly what the issue here is. Is it that my server is having issues, or is it something on my end? I would assume it's more the server's fault, if images/content is being saved quickly sometimes (about 1 second per image when I do a StopWatch), but other times it takes minutes or longer. I will try to get a full StopWatch stacktrace to see what's going on.

It also seems that doing a "thread.sleep(x)" or a ftp disconnect, then connect would sometimes help move the storing of files easier. Not sure if that is just luck, or if that is aiding in some way....

Answers


In Apache Commons Net 3.4 Api setControlKeepAliveTimeout method is described as follows:

Set the time to wait between sending control connection keepalive messages when processing file upload or download.

So, if you want to keep a connection alive, you should try to decrease the waiting time between control messages.

And again stated in API definition, this feature is enabled in the following methods only:

  • retrieveFile(String, OutputStream)
  • appendFile(String, InputStream)
  • storeFile(String, InputStream)
  • storeUniqueFile(InputStream)
  • storeUniqueFileStream(String)

and not in

retrieveFileStream(String) , storeFileStream(String) and the other xxxFileStream methods.

Last note: To upload byte image files you should also set file type and transfer mode as follows:

ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

Hope this helps.


This answer might help some who have a similar program to mine.

Essentially I had to allow the correct permissions in my anti-virus application to allow the storing of files. IT is weird, because as I said before, this worked on my old computer, and the old computer has the same protection...


What I tried:

  1. I tried to alter settings in my code, nothing worked, and some would cause timeouts quickly. This led to me to try an FTPClient and other servers, thinking it was my server

  2. I tried to connect to 2 different servers on my other computer, and had a couple of issues there too. This led me to try and connect to the second server via my code

  3. After having issues with the second server in my code, I realized that it has to be something on my end(weird that server1 would hang for 15 mins, each time it hung, but server2 would have for 10 mins each time.... This led me to try out my Anti-Virus

  4. I tried to pause protection on my AV, as well as shut off the AV program completely, which each gave me a different error. This led me to seek out Windows Firewall to see if there was an issue there

  5. Windows Firewall was on, and by shutting it off, and trying the steps in 4 again, I was about to get it to work.


I am not sure if Windows firewall needs to be up, since I have another Firewall in place with my AV, and I found it weird that Windows Firewall caused issues, seeing as all "Outbound" packets were allowed by default....


Probability.....

1) Sometimes happen that you made changes on listen path in ftp.config file due to that you will get the connection reset issue.

2) Before you tried Java API you can do manually and check the log you will have clear idea.


Things to try :

a.) Close the ftp client after each upload, then reopen the ftp client before you start the next upload. This might avoid any kind of server timeout or network timeout. I know this is inefficient, but server's and networks can have built in timeouts.

b.) Try and use a buffered output stream to the server (if Data or the Apache FTPClient doesn't already do that). A buffered input stream is good, but network latency will happen when trying to "write" to the server.


Need Your Help

SQL-Doesn't return a value when the columns are multiplied

sql multiplying

I have 3 columns in my db table, I need to multiply column, itm_count & itm_price and output the total for a given id(itm_id).

Problems with Executing a DB2 External Stored Procedure

c# stored-procedures db2 ibm-midrange

I am trying to call an external stored procedure (calls an RPG program). I keep getting the following error: