1

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....

Eugène Adell
  • 3,089
  • 2
  • 18
  • 34
XaolingBao
  • 1,034
  • 1
  • 18
  • 34

4 Answers4

3

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.

  • Thanks for the info, didn;'t realize some of those are different i.e., seconds vs milliseconds, and this method is for sending keep alive messages, vs timeout time. This didn't do anything special, I had set it to 10. I did try to do Thread.Sleep(); to see if maybe I was uploading the files too fast, and even though I got more files out of it, at the end, it still crapped out. I did find if(ftp.completePendingCommand()); which might work. – XaolingBao Mar 16 '16 at 03:46
2

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....

XaolingBao
  • 1,034
  • 1
  • 18
  • 34
2

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.

Bhargav Patel
  • 1,098
  • 10
  • 23
1

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.

lincolnadym
  • 909
  • 1
  • 12
  • 29
  • A. I'll see if reconnecting each time does anything I was actually having a similar problem when trying to get a list of files a 2nd time. IT ran into some weird connection closed error, and gave me a ReplyCode of 421. It's weird because my entire application essentially runs wit FTP, and I change directories and all sorts of crap all the time, but I've ran into these 2 weird issues... As for the "timeouts" we are allowed to change them ourselves, but no luck on this end. B. I'm not sure how that would work, as storeFiles' parameters are String and InputStream. – XaolingBao Mar 16 '16 at 01:04
  • Some other things to try : – lincolnadym Mar 18 '16 at 00:03
  • I tried to close and reopen and it seemed to help, but at the same time, after trying ti a bunc of times, it just seems that the connection is slow (see my edit), so the reconnection isn't helping. I did try to use a "storeFileStream" but was met with an error in my output stream being null on the second, or even the first upload, but the upload was much much faster. I had read something random about special characters in usernames/passwords so I'm going to see if that might be an issue. However, my server is ERR_CONNECTION_TIMEOUT so I cannot do anything atm. – XaolingBao Mar 18 '16 at 00:08
  • I have a feeling that my host (Daily Razor) is the cause of this. I have some faint memories of FTP sessions reconnecting all of the time the past few times I tried it, so I think I'm going to try again and see. PITA, but at least if it's my host, I can (hopefully) switch rather simply. – XaolingBao Mar 18 '16 at 00:09
  • Having used Apache's FTPClient for a number of things, I find issues like this have less to do with the code and more to do with the network and servers. Some other things to try : a.) Explicitly set the port. While ftp should default to 21 and sftp should default to 22, you should set the port based on ftp/sftp. b.) double check the server's port's...i.e. telnet . Oddities like "working before but not now" and "it works sometimes" could indicate the ftp/sftp service on the server is crashing and then getting restarted. – lincolnadym Mar 18 '16 at 00:10
  • Thanks for the help man, I appreciate it! So I tried to FTP, and right away it freezes and I get "Timeout detected. Copying files to remote side failed. " This is through WinSCP, which I've used plenty of times, so I have a feelng that my host could be the problem, and most likely is. I cannot log into CP, and uploading is takign awhile. Just got another error... Thinking more it's my host now... Damnit..... Probably should have checked this first, but it was working fine originally, but I guess things happen lol! Good learning experience for sure. I will try the other suggestions tho. – XaolingBao Mar 18 '16 at 00:15
  • Also, since you know what you're doing, I'm curious about which "timeouts" we should set? There is Data, Connection, SOTimeout, Default, KeepAlivex2, and a few others..... I'm not sure what's important/needed. Thanks so much for the help thus far! – XaolingBao Mar 18 '16 at 00:15
  • RIF, Reading is Fundamental. Lol, I know that's not what you need to hear. Start with Daily Razor. If they are providing an ftp service they MUST (or should) have a help page on how it's configured. From there, read up on each type of timeout and try it. If all else fails, trial and error. – lincolnadym Mar 18 '16 at 00:56
  • I understand. I figured asking someone who seems to know about the subject, would be best to get answers, or even resources to "RIF" :P. I contacte them, and they aren't very helpful, but I did try another server, and it seems the same issue persists. The only difference is DR server takes 15 mins of hang time, whereas the other site was only 10. DR is specifically 15 mins each time for some reason. Also, this is really weird, but I tried to see if my AV's firewall mioght cause the problem, and disabling the AV caused the FTP connection to fail, so no testing at all.. Why would that be? – XaolingBao Mar 21 '16 at 23:23
  • Probably nothing, but do you have a try {} catch {} around the code? I don't see it in the above snippet. If not, can you put one and in the catch (Exception ltheXcp) { ltheXcp.printStackTrace(); }? It's important that you review the "entire" stack trace, as it's showing you ALL the Java classes he's running... Finally, acronyms are what? DR -Disaster Recovery? AV- Audio Video? – lincolnadym Mar 21 '16 at 23:50
  • Also, read this post on setBufferSize()...http://stackoverflow.com/questions/11572588/speed-up-apache-commons-ftpclient-transfer . Besides tinkering with timeouts, you should fiddle with setBufferSize(). – lincolnadym Mar 22 '16 at 00:02
  • Yeah I set the buffer size to 1024*1024. I figured out my issue, but haven't completely fixed it yet, just waiting to learn what I need to do with my AV... I wonder if this is an issue for a lot of people...? – XaolingBao Mar 22 '16 at 00:45
  • Yes, ftp is a tricky issue. See this post as well...speaks about differing performance based on the JRE/JDK version : http://stackoverflow.com/questions/14000341/why-is-ftp-upload-slow-in-java-7 . Might be worth dropping to Java 6 and see if it doesn't run better. – lincolnadym Mar 22 '16 at 01:09
  • I just meant ftp issues with Anti-Virus/Firewalls :). I'm using Java8, I heard there was some funky bugs with Java7, and having to run cmd in order to fix it.. – XaolingBao Mar 22 '16 at 05:28