KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FtpDownload


1 import net.sf.jftp.net.ConnectionHandler;
2 import net.sf.jftp.net.ConnectionListener;
3 import net.sf.jftp.net.DataConnection;
4 import net.sf.jftp.net.FtpConnection;
5 import net.sf.jftp.net.BasicConnection;
6 import net.sf.jftp.util.Log;
7 import net.sf.jftp.util.Logger;
8 import net.sf.jftp.config.Settings;
9
10 import java.io.*;
11
12 // this class download a file via anonymous ftp and shows output.
13
//
14
// if you want to use the api in a more complex way, please do at least take a look at the
15
// FtpConnection, FtpTransfer, ConnectionHandler, DirPanel (blockedTransfer, transfer)
16
// and ConnectionListener sourcecode.
17
public class FtpDownload implements Logger, ConnectionListener
18 {
19
20  // is the connection established?
21
private boolean isThere = false;
22
23  public static long time = 0;
24
25  // connection pool, not necessary but you should take a look at this class
26
// if you want to use multiple event based ftp transfers.
27
private ConnectionHandler handler = new ConnectionHandler();
28
29 //creates a FtpConnection and downloads a file
30
public FtpDownload(String JavaDoc host, String JavaDoc file)
31  {
32     // the ftp client default is very small, you may want to increase this
33
Settings.bufferSize = 16384;
34
35     long current = System.currentTimeMillis();
36     //System.out.println("1) "+(System.currentTimeMillis()-current)+"ms.");
37

38     // register app as Logger, debug() and debugRaw below are from now on called by
39
// FtpConnection
40
Log.setLogger(this);
41
42     // create a FtpConnection - note that it does *not* connect instantly
43
FtpConnection con = new FtpConnection(host);
44
45     //System.out.println("2) "+(System.currentTimeMillis()-current)+"ms.");
46

47     // set updatelistener, interface methods are below
48
con.addConnectionListener(this);
49
50     // set handler
51
con.setConnectionHandler(handler);
52
53     // connect and login. this is from where connectionFailed() may be called for example
54
con.login("cdemon","........");
55
56     //System.out.println("3) "+(System.currentTimeMillis()-current)+"ms.");
57

58     // login calls connectionInitialized() below which sets isThere to true
59
while(!isThere)
60     {
61         try { Thread.sleep(10); }
62         catch(Exception JavaDoc ex) { ex.printStackTrace(); }
63     }
64
65     //System.out.println("4) "+(System.currentTimeMillis()-current)+"ms.");
66

67     // download the file - this method blocks until the download has finished
68
// if you want non-blocking, multithreaded io, just use
69
//
70
// con.handleDownload(file);
71
//
72
// which spawns a new thread for the download
73
con.download(file);
74
75     time = (System.currentTimeMillis()-current);
76
77     System.out.println("Download took "+time+"ms.");
78  }
79
80  // download welcome.msg from sourceforge or any other given file
81
public static void main(String JavaDoc argv[])
82  {
83     if(argv.length < 2)
84     {
85         //FtpDownload f = new FtpDownload("ftp.kernel.org", "/welcome.msg");
86

87         long x = 0;
88
89         for(int i=0;i<5;i++) {
90             FtpDownload f = new FtpDownload("localhost", "dsm.pdf");
91             x += f.time;
92         }
93
94         System.out.println("5 runs took "+x+" ms, "+(long) x/5+" ms average.");
95     }
96     else
97     {
98         FtpDownload f = new FtpDownload(argv[0], argv[1]);
99     }
100  }
101
102 // ------------------ needed by ConnectionListener interface -----------------
103

104 // called if the remote directory has changed
105
public void updateRemoteDirectory(BasicConnection con)
106  {
107     System.out.println("new path is: " + con.getPWD());
108  }
109
110  // called if a connection has been established
111
public void connectionInitialized(BasicConnection con)
112  {
113     isThere = true;
114  }
115  
116  // called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)
117
public void updateProgress(String JavaDoc file, String JavaDoc type, long bytes) {}
118
119  // called if connection fails
120
public void connectionFailed(BasicConnection con, String JavaDoc why) {System.out.println("connection failed!");}
121
122  // up- or download has finished
123
public void actionFinished(BasicConnection con) {}
124
125
126  // ------------ needed by Logger interface --------------
127

128     // main log method
129
public void debug(String JavaDoc msg) {} //{System.out.println(msg);}
130

131     // rarely used
132
public void debugRaw(String JavaDoc msg) {}//System.out.print(msg);}
133

134     // methods below are not used yet.
135

136         public void debug(String JavaDoc msg, Throwable JavaDoc throwable) {}
137
138         public void warn(String JavaDoc msg) {}
139
140         public void warn(String JavaDoc msg, Throwable JavaDoc throwable) {}
141
142         public void error(String JavaDoc msg) {}
143
144         public void error(String JavaDoc msg, Throwable JavaDoc throwable) {}
145
146      public void info(String JavaDoc msg) {}
147
148         public void info(String JavaDoc msg, Throwable JavaDoc throwable) {}
149
150         public void fatal(String JavaDoc msg) {}
151
152         public void fatal(String JavaDoc msg, Throwable JavaDoc throwable) {}
153
154 }
155
Popular Tags