View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Sample class to download LIST and MLSD listings from list of ftp sites.
40   */
41  public class DownloadListings extends FTPClient {
42  
43      // Also used by MLDSComparison
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(); // this is reset by connect
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;// = "ftp.funet.fi";
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 }