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 public class FtpDownload implements Logger, ConnectionListener 18 { 19 20 private boolean isThere = false; 22 23 public static long time = 0; 24 25 private ConnectionHandler handler = new ConnectionHandler(); 28 29 public FtpDownload(String host, String file) 31 { 32 Settings.bufferSize = 16384; 34 35 long current = System.currentTimeMillis(); 36 38 Log.setLogger(this); 41 42 FtpConnection con = new FtpConnection(host); 44 45 47 con.addConnectionListener(this); 49 50 con.setConnectionHandler(handler); 52 53 con.login("cdemon","........"); 55 56 58 while(!isThere) 60 { 61 try { Thread.sleep(10); } 62 catch(Exception ex) { ex.printStackTrace(); } 63 } 64 65 67 con.download(file); 74 75 time = (System.currentTimeMillis()-current); 76 77 System.out.println("Download took "+time+"ms."); 78 } 79 80 public static void main(String argv[]) 82 { 83 if(argv.length < 2) 84 { 85 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 104 public void updateRemoteDirectory(BasicConnection con) 106 { 107 System.out.println("new path is: " + con.getPWD()); 108 } 109 110 public void connectionInitialized(BasicConnection con) 112 { 113 isThere = true; 114 } 115 116 public void updateProgress(String file, String type, long bytes) {} 118 119 public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");} 121 122 public void actionFinished(BasicConnection con) {} 124 125 126 128 public void debug(String msg) {} 131 public void debugRaw(String msg) {} 134 136 public void debug(String msg, Throwable throwable) {} 137 138 public void warn(String msg) {} 139 140 public void warn(String msg, Throwable throwable) {} 141 142 public void error(String msg) {} 143 144 public void error(String msg, Throwable throwable) {} 145 146 public void info(String msg) {} 147 148 public void info(String msg, Throwable throwable) {} 149 150 public void fatal(String msg) {} 151 152 public void fatal(String msg, Throwable throwable) {} 153 154 } 155 | Popular Tags |