|
14 | 14 | * See the License for the specific language governing permissions and
|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 |
| - |
| 17 | + |
18 | 18 | package examples.ftp;
|
19 | 19 |
|
20 | 20 | import java.io.FileInputStream;
|
21 | 21 | import java.io.FileOutputStream;
|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.io.InputStream;
|
24 | 24 | import java.io.OutputStream;
|
25 |
| -import java.io.PrintStream; |
26 | 25 | import java.io.PrintWriter;
|
27 | 26 |
|
28 | 27 | import org.apache.commons.net.PrintCommandListener;
|
29 | 28 | import org.apache.commons.net.ftp.FTP;
|
30 | 29 | import org.apache.commons.net.ftp.FTPClient;
|
31 | 30 | import org.apache.commons.net.ftp.FTPConnectionClosedException;
|
32 |
| -import org.apache.commons.net.ftp.FTPFile; |
33 | 31 | import org.apache.commons.net.ftp.FTPReply;
|
34 | 32 |
|
35 | 33 | /***
|
|
42 | 40 | * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
|
43 | 41 | * <p>
|
44 | 42 | ***/
|
45 |
| -public final class FTPExample { |
| 43 | +public final class FTPExample |
| 44 | +{ |
| 45 | + |
| 46 | + public static final String USAGE = |
| 47 | + "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" + |
| 48 | + "\nDefault behavior is to download a file and use ASCII transfer mode.\n" + |
| 49 | + "\t-s store file on server (upload)\n" + |
| 50 | + "\t-b use binary transfer mode\n"; |
46 | 51 |
|
47 |
| - public static final void main(String[] args) throws IOException |
| 52 | + public static final void main(String[] args) |
48 | 53 | {
|
49 |
| - FTPClient client = new FTPClient(); |
50 |
| - client.setControlEncoding("utf-8"); |
51 |
| - client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); |
52 |
| - client.connect("localhost"); |
53 |
| - client.login("rory", "yi6bovak"); |
54 |
| - |
55 |
| - PrintStream out = new PrintStream(System.out, true, "utf-8"); |
56 |
| - |
57 |
| - for (FTPFile file : client.listFiles()) |
58 |
| - out.println(file.getName()); |
59 |
| - |
60 |
| - out.println("你好吗"); |
| 54 | + int base = 0; |
| 55 | + boolean storeFile = false, binaryTransfer = false, error = false; |
| 56 | + String server, username, password, remote, local; |
| 57 | + FTPClient ftp; |
| 58 | + |
| 59 | + for (base = 0; base < args.length; base++) |
| 60 | + { |
| 61 | + if (args[base].startsWith("-s")) |
| 62 | + storeFile = true; |
| 63 | + else if (args[base].startsWith("-b")) |
| 64 | + binaryTransfer = true; |
| 65 | + else |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + if ((args.length - base) != 5) |
| 70 | + { |
| 71 | + System.err.println(USAGE); |
| 72 | + System.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + server = args[base++]; |
| 76 | + username = args[base++]; |
| 77 | + password = args[base++]; |
| 78 | + remote = args[base++]; |
| 79 | + local = args[base]; |
| 80 | + |
| 81 | + ftp = new FTPClient(); |
| 82 | + ftp.addProtocolCommandListener(new PrintCommandListener( |
| 83 | + new PrintWriter(System.out))); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + int reply; |
| 88 | + ftp.connect(server); |
| 89 | + System.out.println("Connected to " + server + "."); |
| 90 | + |
| 91 | + // After connection attempt, you should check the reply code to verify |
| 92 | + // success. |
| 93 | + reply = ftp.getReplyCode(); |
| 94 | + |
| 95 | + if (!FTPReply.isPositiveCompletion(reply)) |
| 96 | + { |
| 97 | + ftp.disconnect(); |
| 98 | + System.err.println("FTP server refused connection."); |
| 99 | + System.exit(1); |
| 100 | + } |
| 101 | + } |
| 102 | + catch (IOException e) |
| 103 | + { |
| 104 | + if (ftp.isConnected()) |
| 105 | + { |
| 106 | + try |
| 107 | + { |
| 108 | + ftp.disconnect(); |
| 109 | + } |
| 110 | + catch (IOException f) |
| 111 | + { |
| 112 | + // do nothing |
| 113 | + } |
| 114 | + } |
| 115 | + System.err.println("Could not connect to server."); |
| 116 | + e.printStackTrace(); |
| 117 | + System.exit(1); |
| 118 | + } |
| 119 | + |
| 120 | +__main: |
| 121 | + try |
| 122 | + { |
| 123 | + if (!ftp.login(username, password)) |
| 124 | + { |
| 125 | + ftp.logout(); |
| 126 | + error = true; |
| 127 | + break __main; |
| 128 | + } |
| 129 | + |
| 130 | + System.out.println("Remote system is " + ftp.getSystemName()); |
| 131 | + |
| 132 | + if (binaryTransfer) |
| 133 | + ftp.setFileType(FTP.BINARY_FILE_TYPE); |
| 134 | + |
| 135 | + // Use passive mode as default because most of us are |
| 136 | + // behind firewalls these days. |
| 137 | + ftp.enterLocalPassiveMode(); |
| 138 | + |
| 139 | + if (storeFile) |
| 140 | + { |
| 141 | + InputStream input; |
| 142 | + |
| 143 | + input = new FileInputStream(local); |
| 144 | + |
| 145 | + ftp.storeFile(remote, input); |
| 146 | + |
| 147 | + input.close(); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + OutputStream output; |
| 152 | + |
| 153 | + output = new FileOutputStream(local); |
| 154 | + |
| 155 | + ftp.retrieveFile(remote, output); |
| 156 | + |
| 157 | + output.close(); |
| 158 | + } |
| 159 | + |
| 160 | + ftp.logout(); |
| 161 | + } |
| 162 | + catch (FTPConnectionClosedException e) |
| 163 | + { |
| 164 | + error = true; |
| 165 | + System.err.println("Server closed connection."); |
| 166 | + e.printStackTrace(); |
| 167 | + } |
| 168 | + catch (IOException e) |
| 169 | + { |
| 170 | + error = true; |
| 171 | + e.printStackTrace(); |
| 172 | + } |
| 173 | + finally |
| 174 | + { |
| 175 | + if (ftp.isConnected()) |
| 176 | + { |
| 177 | + try |
| 178 | + { |
| 179 | + ftp.disconnect(); |
| 180 | + } |
| 181 | + catch (IOException f) |
| 182 | + { |
| 183 | + // do nothing |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + System.exit(error ? 1 : 0); |
61 | 189 | } // end main
|
62 | 190 |
|
63 | 191 | }
|
|
0 commit comments