KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FtpUpload


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 /**
13 * See FtpDownload.java for comments.
14 */

15 public class FtpUpload implements Logger, ConnectionListener
16 {
17
18  private boolean isThere = false;
19
20  private ConnectionHandler handler = new ConnectionHandler();
21
22  public FtpUpload(String JavaDoc host, String JavaDoc dir, String JavaDoc file)
23  {
24     Log.setLogger(this);
25
26     FtpConnection con = new FtpConnection(host);
27
28     con.addConnectionListener(this);
29
30     con.setConnectionHandler(handler);
31
32     con.login("anonymous","no@no.no");
33
34     while(!isThere)
35     {
36         try { Thread.sleep(10); }
37         catch(Exception JavaDoc ex) { ex.printStackTrace(); }
38     }
39
40     con.chdir(dir);
41
42     con.upload(file);
43  }
44
45  public static void main(String JavaDoc argv[])
46  {
47     if(argv.length == 3)
48     {
49         FtpUpload f = new FtpUpload(argv[0], argv[2], argv[1]);
50     }
51     else
52     {
53      FtpUpload g =
54         new FtpUpload("upload.sourceforge.net", "/incoming", "test.txt");
55     }
56 }
57
58
59  public void updateRemoteDirectory(BasicConnection con)
60  {
61     System.out.println("new path is: " + con.getPWD());
62  }
63  
64  public void connectionInitialized(BasicConnection con)
65  {
66     isThere = true;
67  }
68  
69  public void updateProgress(String JavaDoc file, String JavaDoc type, long bytes) {}
70  
71  public void connectionFailed(BasicConnection con, String JavaDoc why) {System.out.println("connection failed!");}
72
73  public void actionFinished(BasicConnection con) {}
74
75     public void debug(String JavaDoc msg) {System.out.println(msg);}
76
77      public void debugRaw(String JavaDoc msg) {System.out.print(msg);}
78
79     public void debug(String JavaDoc msg, Throwable JavaDoc throwable) {}
80
81     public void warn(String JavaDoc msg) {}
82
83     public void warn(String JavaDoc msg, Throwable JavaDoc throwable) {}
84
85     public void error(String JavaDoc msg) {}
86
87     public void error(String JavaDoc msg, Throwable JavaDoc throwable) {}
88
89     public void info(String JavaDoc msg) {}
90
91     public void info(String JavaDoc msg, Throwable JavaDoc throwable) {}
92
93     public void fatal(String JavaDoc msg) {}
94
95     public void fatal(String JavaDoc msg, Throwable JavaDoc throwable) {}
96
97 }
98
Popular Tags