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