Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
9k views
in Java FTP by (1k points)
Hi,

I'm using edtftpj free version in my java project.

there is an issue we want to sort out.
suppose the following senario :

files are being uploaded to a ftp server (in a separate java thread).then user brutally unplug the lan cable and cutt of network connection...
after few secs user plug the cable again .
Now we need to be able to resume uploading files from point where the network connection went down.

what i did is the following :

i catch exception that is throw when connection is broken ,then i call :
client.resume();


but this doesn't work ,many exceptions are thrown.the exception trace is below.

can someone pint me how to come out right with this issue?

thanks a lot.



DEBUG [FTPControlSocket] 5 Jul 2007 09:25:59.409 : 226 Transfer finished successfully. Data connection closed.
DEBUG [FTPControlSocket] 5 Jul 2007 09:25:59.409 : ---> CWD /staging/Projects/12/View/_Thumbs
DEBUG [FTPControlSocket] 5 Jul 2007 09:25:59.471 : 250-[Ul: 58.61MB] [Dl: 0.00MB] [Speed: UL:45,DL:0 KB/s] [Space: 355042MB]
DEBUG [FTPControlSocket] 5 Jul 2007 09:25:59.471 : 250-[Credits: UnlimitedMB] [Ratio: Unlimited]
java.net.SocketException: Connection reset
   at java.net.SocketInputStream.read(Unknown Source)
   at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
   at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
   at sun.nio.cs.StreamDecoder.read(Unknown Source)
   at java.io.InputStreamReader.read(Unknown Source)
   at java.io.BufferedReader.fill(Unknown Source)
   at java.io.BufferedReader.readLine(Unknown Source)
   at java.io.BufferedReader.readLine(Unknown Source)
   at com.enterprisedt.net.ftp.FTPControlSocket.readReply(FTPControlSocket.java:825)DEBUG [upload.FTPUploadThread] 5 Jul 2007 09:26:08.612 : Error processing thumbnails 'A-group005.jpg'at 

14 Answers

0 votes
by (162k points)
When you catch the exception, call quitImmediately() and reconnect and login. Then call resume() before getting the file and all should work.
0 votes
by (1k points)
When you catch the exception, call quitImmediately() and reconnect and login. Then call resume() before getting the file and all should work.


thanks for reply;
this is exactly what i was doing at first & did not worked .the difference is that i was calling quit() instead of quitImmediately() , then i reconnect ,resume() and get file; but this was not working.
after this i removed the call for quit() and reconnect() & only keeped resume() , but sill not working.
is there a difference between quit() and quitImmediately() ? do you think this method was the source of problem ?

anyway i'll try this solution definitely & let you know .

thanks.
0 votes
by (1k points)
i tried this solution but no luck !

when i catch an exception i call this method :
private synchronized void retry(File file) throws FileUploadException {
      
      while (attempts < r.MAXRETRIES) {
         util.Trace.log("File transfer failure ! reconnecting (attempt "
               + attempts + ")...");

         try {
            client.quitImmediately();            
            makeConnection();
            client.resume();
            putFile(file);
            break;
         } catch (Exception e) {
            attempts++;
            System.out.println("attempts="+attempts);
            continue;
         }

      }
      if (attempts == r.MAXRETRIES) {
         throw new FileUploadException("File Upload Failed ");

      }
      
   }


any Ideas how to solve this ?

thanks.


debug trace:


----------
DEBUG [FTPControlSocket] 5 Jul 2007 19:17:15.256 : ---> PASV
DEBUG [FTPControlSocket] 5 Jul 2007 19:17:15.334 : 227 Entering Passive Mode (209,203,246,34,6,242)
DEBUG [FTPControlSocket] 5 Jul 2007 19:17:15.381 : ---> STOR A-group006.jpg
DEBUG [FTPControlSocket] 5 Jul 2007 19:17:15.428 : 150 File status okay; about to open data connection.
DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:15.428 : 16384 transferred
DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:15.428 : 32768 transferred
DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:15.444 : 49152 transferred
DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:15.444 : 65536 transferred
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at com.enterprisedt.net.ftp.FTPClient.putData(FTPClient.java:1896)
at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1595)
at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1568)ERROR [FTPClient] 5 Jul 2007 19:17:23.131 : Caught and

------------------------------------------------------

DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:26.444 : connecting to server...
DEBUG [FTPClient] 5 Jul 2007 19:17:26.444 : Connecting to /209.203.246.34:21
DEBUG [SocketUtils] 5 Jul 2007 19:17:26.444 : Invoking connect with timeout=60000
java.net.NoRouteToHostException: No route to host: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)DEBUG [upload.FTPUploadThread] 5 Jul 2007 19:17:28.444 : Problems connecting to ftp server: No route to host: connect
[/code]
0 votes
by (162k points)
You are getting "java.net.NoRouteToHostException: No route to host: connect " on the reconnect because you are trying to reconnect before you plug the cable back in.

Also normally in a situation like this you should put in a sleep for say 10 s between each connection attempt.
0 votes
by (1k points)
It seems the applet is now reconnecting properly. The only problem is it cannot find the file to upload (in putFile() method) .
But i'm sure i'm not deleting the File objects .i did that only at the end of the run() thread method which runs the thread for uploading a collection of files.

if the connection is cut ;what will happen to the run() thread method ? I don't know what the File not found exception means.

here is the method for uploading :

private synchronized void putFile(File file) throws FileUploadException {
      InputStream in = null;
      try {
         in = new FileInputStream(file);
         client.put(in, file.getName());
         r.numBytes += file.length();
         in.close();

      } catch (Exception e) {
         e.printStackTrace();
         try {
            //client.cancelTransfer();
            in.close();
            retry(file);
         } catch (Exception e1) {
            throw new FileUploadException(e1.getMessage());

         } 
      }

   }


and here is the log console trace :

DEBUG [FTPControlSocket] 6 Jul 2007 10:40:33.991 : 227 Entering Passive Mode (209,203,246,34,10,167)
DEBUG [FTPControlSocket] 6 Jul 2007 10:40:34.38 : ---> SIZE A_group001.jpg
com.enterprisedt.net.ftp.FTPException: File not found
   at com.enterprisedt.net.ftp.FTPControlSocket.validateReply(FTPControlSocket.java:949)
   at com.enterprisedt.net.ftp.FTPClient.size(FTPClient.java:1486)
   at com.enterprisedt.net.ftp.FTPClient.initPut(FTPClient.java:1768)
   at com.enterprisedt.net.ftp.FTPClient.putData(FTPClient.java:1838)
   at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1595)
   at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1568)
   at upload.FTPUploadThread.putFile(FTPUploadThread.java:454)
   at upload.FTPUploadThread.retry(FTPUploadThread.java:483)DEBUG [FTPControlSocket] 6 Jul 2007 10:40:34.84 : 550 File not found
INFO [FTPControlSocket] 6 Jul 2007 10:40:34.84 : Expected reply code = [213]
0 votes
by (162k points)
The error is that when size() is called on the *remote* file A_group001.jpg it wasn't found. Not the local file.
0 votes
by (1k points)
The error is that when size() is called on the *remote* file A_group001.jpg it wasn't found. Not the local file.


the File Not Found error point me to the following line in the above putFile(File f) method :
client.put(in, file.getName());


does the put() method indirectly call size() on remote file ?

it's obvious that the remote file is not yet completely uploaded to server when the connection broke down ; but if i call resume() it is supposed to continue uploading right ?

why this error is thrown ? is it because i create a new
 FileInputStream in 
and pass it again to put(in,file) ?

i still can't figure out the cause of the problem correctly .i would appreciate if you help on this issue .

thanks.
0 votes
by (162k points)
Yes, the put() method calls size() when you try to resume() - as it must to know how much was transfer so it knows where to resume from.

However if the file doesn't exist on the server (as is the case here), there is nothing to resume.

Best to call exists() first on the filename, and only call resume() if the file exists.

However we'll consider changing put() to ignore this error if the file does not exist - as it should probably do.
0 votes
by (1k points)
I'm solving issues progressively for this reconnection problem.each time i solve one issue i get another different problem !

it seems now that the File Not found exception is solved ; but now i have another exception when applet tries to resume upload .
it's a can't access file error (see debug trace below).it seems like the ftp server is locking the remote file or something like that.

how can i solve this new problem ?

thanks again for your support.


DEBUG [FTPControlSocket] 7 juil. 2007 15:51:05.343 : 550 can't access file.
INFO [FTPControlSocket] 7 juil. 2007 15:51:05.343 : Expected reply codes = [225,226,250,426,450]
WARN [FTPClient] 7 juil. 2007 15:51:05.343 : Validate transfer on error failed : can't access file.
com.enterprisedt.net.ftp.FTPException: can't access file.
   at com.enterprisedt.net.ftp.FTPControlSocket.validateReply(FTPControlSocket.java:927)
   at com.enterprisedt.net.ftp.FTPClient.validateTransfer(FTPClient.java:1629)
   at com.enterprisedt.net.ftp.FTPClient.validateTransferOnError(FTPClient.java:1653)
   at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1600)
   at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1580)
   at com.enterprisedt.net.ftp.FTPClient.put(FTPClient.java:1558)
   at upload.FTPUploadThread.putFile(FTPUploadThread.java:416)
   at upload.FTPUploadThread.retry(FTPUploadThread.java:444)
   at upload.FTPUploadThread.putFile(FTPUploadThread.java:423)
   at upload.FTPUploadThread.uploadFile(FTPUploadThread.java:135)
   at upload.FTPUploadThread.run(FTPUploadThread.java:544)
0 votes
by (162k points)
It looks like you will need to disconnect and reconnect before you try to resume.

Categories

...