1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.net.ftp.parser;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.PrintWriter;
30 import java.io.Reader;
31 import java.net.Socket;
32
33 import org.apache.commons.net.PrintCommandListener;
34 import org.apache.commons.net.ftp.FTPClient;
35 import org.apache.commons.net.ftp.FTPCmd;
36 import org.apache.commons.net.io.Util;
37
38
39
40
41 public class DownloadListings extends FTPClient {
42
43
44 static final String DOWNLOAD_DIR = "target/ftptest";
45
46 private PrintCommandListener listener;
47 private PrintWriter out;
48
49 private boolean open(String host, int port) throws Exception{
50 System.out.println("Connecting to "+host);
51 out = new PrintWriter(new FileWriter(new File(DOWNLOAD_DIR, host+"_info.txt")));
52 listener = new PrintCommandListener(out);
53 addProtocolCommandListener(listener);
54 setConnectTimeout(30000);
55 try {
56 connect(host, port);
57 } catch (Exception e) {
58 System.out.println(e);
59 return false;
60 }
61 enterLocalPassiveMode();
62 System.out.println("Logging in to "+host);
63 return login("anonymous", "user@localhost");
64 }
65
66 private void info() throws IOException {
67 syst();
68 help();
69 feat();
70 removeProtocolCommandListener(listener);
71 }
72
73 private void download(String path, FTPCmd command, File filename) throws Exception {
74 Socket socket;
75 if ((socket = _openDataConnection_(command, getListArguments(path))) == null) {
76 System.out.println(getReplyString());
77 return;
78 }
79 InputStream inputStream = socket.getInputStream();
80 OutputStream outputStream = new FileOutputStream(filename);
81 Util.copyStream(inputStream, outputStream );
82 inputStream.close();
83 socket.close();
84 outputStream.close();
85
86 if (!completePendingCommand())
87 {
88 System.out.println(getReplyString());
89 }
90 }
91
92 public static void main(String[] args) throws Exception {
93 String host;
94 int port = 21;
95 String path;
96
97 new File(DOWNLOAD_DIR).mkdirs();
98 DownloadListings self = new DownloadListings();
99 OutputStream os = new FileOutputStream(new File(DOWNLOAD_DIR, "session.log"));
100 self.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(os), true));
101
102 Reader is = new FileReader("mirrors.list");
103 BufferedReader rdr = new BufferedReader(is);
104 String line;
105 while((line=rdr.readLine()) != null){
106 if (line.startsWith("ftp")){
107 String []parts = line.split("\\s+");
108 String target = parts[2];
109 host = target.substring("ftp://".length());
110 int slash = host.indexOf('/');
111 path = host.substring(slash);
112 host = host.substring(0,slash);
113 System.out.println(host+ " "+path);
114 if (self.open(host, port)) {
115 try {
116 self.info();
117 self.download(path, FTPCmd.LIST, new File(DOWNLOAD_DIR, host+"_list.txt"));
118 self.download(path, FTPCmd.MLSD, new File(DOWNLOAD_DIR, host+"_mlsd.txt"));
119 } catch (Exception e) {
120 e.printStackTrace();
121 } finally {
122 self.disconnect();
123 }
124 self.removeProtocolCommandListener(self.listener);
125 self.out.close();
126 }
127 }
128 }
129 os.close();
130 rdr.close();
131 }
132 }