001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package examples; 019 020 import java.io.FileInputStream; 021 import java.io.FileOutputStream; 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.io.OutputStream; 025 import java.io.PrintWriter; 026 import java.security.NoSuchAlgorithmException; 027 028 import org.apache.commons.net.PrintCommandListener; 029 import org.apache.commons.net.ftp.FTP; 030 import org.apache.commons.net.ftp.FTPConnectionClosedException; 031 import org.apache.commons.net.ftp.FTPReply; 032 import org.apache.commons.net.ftp.FTPSClient; 033 034 /*** 035 * This is an example program demonstrating how to use the FTPSClient class. 036 * This program connects to an FTP server and retrieves the specified 037 * file. If the -s flag is used, it stores the local file at the FTP server. 038 * Just so you can see what's happening, all reply strings are printed. 039 * If the -b flag is used, a binary transfer is assumed (default is ASCII). 040 * <p> 041 * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file> 042 * <p> 043 ***/ 044 public final class FTPSExample 045 { 046 047 public static final String USAGE = 048 "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" + 049 "\nDefault behavior is to download a file and use ASCII transfer mode.\n" + 050 "\t-s store file on server (upload)\n" + 051 "\t-b use binary transfer mode\n"; 052 053 public static final void main(String[] args) throws NoSuchAlgorithmException 054 { 055 int base = 0; 056 boolean storeFile = false, binaryTransfer = false, error = false; 057 String server, username, password, remote, local; 058 String protocol = "SSL"; // SSL/TLS 059 FTPSClient ftps; 060 061 for (base = 0; base < args.length; base++) 062 { 063 if (args[base].startsWith("-s")) 064 storeFile = true; 065 else if (args[base].startsWith("-b")) 066 binaryTransfer = true; 067 else 068 break; 069 } 070 071 if ((args.length - base) != 5) 072 { 073 System.err.println(USAGE); 074 System.exit(1); 075 } 076 077 server = args[base++]; 078 username = args[base++]; 079 password = args[base++]; 080 remote = args[base++]; 081 local = args[base]; 082 083 ftps = new FTPSClient(protocol); 084 085 ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 086 087 try 088 { 089 int reply; 090 091 ftps.connect(server); 092 System.out.println("Connected to " + server + "."); 093 094 // After connection attempt, you should check the reply code to verify 095 // success. 096 reply = ftps.getReplyCode(); 097 098 if (!FTPReply.isPositiveCompletion(reply)) 099 { 100 ftps.disconnect(); 101 System.err.println("FTP server refused connection."); 102 System.exit(1); 103 } 104 } 105 catch (IOException e) 106 { 107 if (ftps.isConnected()) 108 { 109 try 110 { 111 ftps.disconnect(); 112 } 113 catch (IOException f) 114 { 115 // do nothing 116 } 117 } 118 System.err.println("Could not connect to server."); 119 e.printStackTrace(); 120 System.exit(1); 121 } 122 123 __main: 124 try 125 { 126 ftps.setBufferSize(1000); 127 128 if (!ftps.login(username, password)) 129 { 130 ftps.logout(); 131 error = true; 132 break __main; 133 } 134 135 136 System.out.println("Remote system is " + ftps.getSystemName()); 137 138 if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE); 139 140 // Use passive mode as default because most of us are 141 // behind firewalls these days. 142 ftps.enterLocalPassiveMode(); 143 144 if (storeFile) 145 { 146 InputStream input; 147 148 input = new FileInputStream(local); 149 150 ftps.storeFile(remote, input); 151 152 input.close(); 153 } 154 else 155 { 156 OutputStream output; 157 158 output = new FileOutputStream(local); 159 160 ftps.retrieveFile(remote, output); 161 162 output.close(); 163 } 164 165 ftps.logout(); 166 } 167 catch (FTPConnectionClosedException e) 168 { 169 error = true; 170 System.err.println("Server closed connection."); 171 e.printStackTrace(); 172 } 173 catch (IOException e) 174 { 175 error = true; 176 e.printStackTrace(); 177 } 178 finally 179 { 180 if (ftps.isConnected()) 181 { 182 try 183 { 184 ftps.disconnect(); 185 } 186 catch (IOException f) 187 { 188 // do nothing 189 } 190 } 191 } 192 193 System.exit(error ? 1 : 0); 194 } // end main 195 196 }