You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by mehmet dongel <md...@yahoo.com> on 2005/05/03 18:57:51 UTC

Timeout Problem

Please Help...
Hi Everyone.
I want to set ftp timeout value by setDataTimeout() method. Download file size 89 MB and takes
20-25 sec (20000-25000 msec) to download. when I use  client.setDataTimeOut(1000) , FtpGet
performed. I expect time out exception or CopyStreamm exception.

Code is below

/*
 * Created on May 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author mehmetd
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.*;




public class FtpMain {

	   
	  
	   
	public void run() throws Exception  {
		FTPClient client = new FTPClient ();
		
		client.connect ("10.56.32.135", 21);
		
        int reply = client.getReplyCode ();
        
        if (false == FTPReply.isPositiveCompletion (reply)) {
            client.disconnect ();
            throw new Exception (
                "Failed to open, server response: " + reply);
        }
        
        client.setDataTimeout (1000);
        if (false == client.login ("anonymous", "anonymous")) {
            throw new Exception ("login failed");
        }
        try {
        client.setFileType (FTPClient.BINARY_FILE_TYPE);
        client.changeWorkingDirectory ("/pub");
        File localFile = new File ("mlocalfile");
        
        FileOutputStream os = null;
        
        try {
            os = new FileOutputStream(localFile);
            try {
                
                long t1 = System.currentTimeMillis ();
            	if (false == client.retrieveFile ("filename", os)) { // file is 89 MB
                    throw new Exception ("File " + "as" + 
                        " couldn't be retrieved. Possible reasons: " +
                        "directory or file does not exist");
                }
                os.flush ();
                long t2 = System.currentTimeMillis ();
                
                System.out.println(t2-t1);
               
            } catch (IOException ioex){
            	ioex.printStackTrace();
            }
            finally {
                client.setDataTimeout (0);
               
                os.close ();
            }
        } catch (Exception e) {
         e.printStackTrace();
        }
        } catch(FTPConnectionClosedException ex){
        	ex.printStackTrace();
        }
		
	}
	
	
	public static void main(String[] args) {
		try {
		new FtpMain().run();
		} catch(Exception ex){
			ex.printStackTrace();
		}
	}
}


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: Timeout Problem

Posted by Mattias J <mj...@expertsystem.se>.
The timeout is not used to limit the total download time, but to limit the 
time the client waits for the server to respond (i.e. start sending data).
AFAIK the JDK does not provide a way for setting a "maximum connection 
time", and after a quick glimpse it seems Commons Net FTP does not provide 
for it either.
So I assume you will have to build this behaviour yourself with a separate 
thread.

At 2005-05-03 18:57, you wrote:
>Please Help...
>Hi Everyone.
>I want to set ftp timeout value by setDataTimeout() method. Download file 
>size 89 MB and takes
>20-25 sec (20000-25000 msec) to download. when I 
>use  client.setDataTimeOut(1000) , FtpGet
>performed. I expect time out exception or CopyStreamm exception.
>
>Code is below
>
>/*
>  * Created on May 3, 2005
>  *
>  * TODO To change the template for this generated file go to
>  * Window - Preferences - Java - Code Style - Code Templates
>  */
>
>/**
>  * @author mehmetd
>  *
>  * TODO To change the template for this generated type comment go to
>  * Window - Preferences - Java - Code Style - Code Templates
>  */
>import java.io.File;
>import java.io.FileOutputStream;
>import java.io.IOException;
>
>import org.apache.commons.net.ftp.*;
>
>
>
>
>public class FtpMain {
>
>
>
>
>         public void run() throws Exception  {
>                 FTPClient client = new FTPClient ();
>
>                 client.connect ("10.56.32.135", 21);
>
>         int reply = client.getReplyCode ();
>
>         if (false == FTPReply.isPositiveCompletion (reply)) {
>             client.disconnect ();
>             throw new Exception (
>                 "Failed to open, server response: " + reply);
>         }
>
>         client.setDataTimeout (1000);
>         if (false == client.login ("anonymous", "anonymous")) {
>             throw new Exception ("login failed");
>         }
>         try {
>         client.setFileType (FTPClient.BINARY_FILE_TYPE);
>         client.changeWorkingDirectory ("/pub");
>         File localFile = new File ("mlocalfile");
>
>         FileOutputStream os = null;
>
>         try {
>             os = new FileOutputStream(localFile);
>             try {
>
>                 long t1 = System.currentTimeMillis ();
>                 if (false == client.retrieveFile ("filename", os)) { // 
> file is 89 MB
>                     throw new Exception ("File " + "as" +
>                         " couldn't be retrieved. Possible reasons: " +
>                         "directory or file does not exist");
>                 }
>                 os.flush ();
>                 long t2 = System.currentTimeMillis ();
>
>                 System.out.println(t2-t1);
>
>             } catch (IOException ioex){
>                 ioex.printStackTrace();
>             }
>             finally {
>                 client.setDataTimeout (0);
>
>                 os.close ();
>             }
>         } catch (Exception e) {
>          e.printStackTrace();
>         }
>         } catch(FTPConnectionClosedException ex){
>                 ex.printStackTrace();
>         }
>
>         }
>
>
>         public static void main(String[] args) {
>                 try {
>                 new FtpMain().run();
>                 } catch(Exception ex){
>                         ex.printStackTrace();
>                 }
>         }
>}
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org