FTP: Difference between revisions

4,417 bytes added ,  9 years ago
(make task description fit the code)
(→‎{{header|Java}}: added Java)
Line 2:
 
Connect to a server, change directory, list its contents and download a file as binary using the FTP protocol. Use passive mode if available.
 
=={{header|Java}}==
<lang java>import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
public class FTPconn {
 
public static void main(String[] args) throws IOException {
String server = "ftp.hq.nasa.gov";
int port = 21;
String user = "anonymous";
String pass = "ftptest@example.com";
 
OutputStream output = null;
 
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
 
serverReply(ftpClient);
 
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("Failure. Server reply code: " + replyCode);
return;
}
 
serverReply(ftpClient);
 
if (!ftpClient.login(user, pass)) {
System.out.println("Could not login to the server.");
return;
}
 
String dir = "pub/issoutreach/Living in Space Stories (MP3 Files)/";
if (!ftpClient.changeWorkingDirectory(dir)) {
System.out.println("Cwd failed.");
return;
}
 
ftpClient.enterLocalPassiveMode();
 
for (FTPFile file : ftpClient.listFiles())
System.out.println(file);
 
String filename = "Can People go to Mars.mp3";
output = new FileOutputStream(filename);
 
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (!ftpClient.retrieveFile(filename, output)) {
System.out.println("Retrieving file failed");
return;
}
 
serverReply(ftpClient);
 
ftpClient.logout();
 
} finally {
if (output != null)
output.close();
}
}
 
private static void serverReply(FTPClient ftpClient) {
for (String reply : ftpClient.getReplyStrings()) {
System.out.println(reply);
}
}
}</lang>
 
Output:
 
<pre>220-Warning: This system is owned and operated by the US Federal Government.
Unauthorized access to this system is a violation of US Federal
law and could lead to prosecution.
This is NASA HQ ANONYMOUS FTP SERVER.
Please read the README file located in the initial server root directory.
IF you place files into the /incoming directory, it is IMPERATIVE that you
notify ftp-admin@hq.nasa.gov that you have done so and of your intended
disposition of those files. Absent such notification, all files placed
in /incoming that cannot be identified will be immediately deleted.
220 FTP Server Ready
220-Warning: This system is owned and operated by the US Federal Government.
Unauthorized access to this system is a violation of US Federal
law and could lead to prosecution.
This is NASA HQ ANONYMOUS FTP SERVER.
Please read the README file located in the initial server root directory.
IF you place files into the /incoming directory, it is IMPERATIVE that you
notify ftp-admin@hq.nasa.gov that you have done so and of your intended
disposition of those files. Absent such notification, all files placed
in /incoming that cannot be identified will be immediately deleted.
220 FTP Server Ready
-rw-rw-r-- 1 109 space-station 2327118 May 9 2005 09sept_spacepropulsion.mp3
-rw-rw-r-- 1 109 space-station 1260304 May 9 2005 Can People go to Mars.mp3
-rw-rw-r-- 1 109 space-station 1350270 May 9 2005 Distill some water.mp3
-rw-rw-r-- 1 109 space-station 1290888 May 9 2005 Good Vibrations.mp3
-rw-rw-r-- 1 109 space-station 1431834 May 9 2005 Gravity Hurts_So good.mp3
-rw-rw-r-- 1 109 space-station 1072644 May 9 2005 Gravity in the Brain.mp3
-rw-rw-r-- 1 109 space-station 1230594 May 9 2005 Power to the ISS.mp3
-rw-rw-r-- 1 109 space-station 1309062 May 9 2005 Space Bones.mp3
-rw-rw-r-- 1 109 space-station 2292715 May 9 2005 Space Power.mp3
-rw-rw-r-- 1 109 space-station 772075 May 9 2005 We have a solution.mp3
-rw-rw-r-- 1 109 space-station 1134654 May 9 2005 When Space Makes you Dizzy.mp3
226 Transfer complete</pre>
 
 
 
=={{header|Python}} 2==
Anonymous user