Beruflich Dokumente
Kultur Dokumente
--> <pre> */
import java.io.*;
import java.util.*;
class TestFTP
{
public static void main (String[] args)
{
String serverName;
FTPConnection ftp = null;
try
{
if (args.length == 0)
{
serverName = getStringFromUser("Enter the server you would
like to connect to: ");
if (serverName.length() == 0) { return; }
} else {
serverName = args[0];
}
if (ftp.login("anonymous", "blah@blah.blah"))
{
System.out.println("Successfully logged in!");
System.out.println("System type is: " +
ftp.getSystemType());
System.out.println("Current directory is: " +
ftp.getCurrentDirectory());
String files = ftp.listFiles();
String subDirs = ftp.listSubdirectories();
System.out.println("Files in Directory:\n" + files);
System.out.println("Subdirectories:\n" + subDirs);
if (sdName.length() > 0)
{
System.out.println("Changing to directory " +
sdName);
if (ftp.changeDirectory(sdName))
{
// just for kicks, try to download the first 3
files in the directory
files = ftp.listFiles();
st = new StringTokenizer(files, ftp.lineTerm);
String fileName;
int count = 1;
while ((st.hasMoreTokens()) && (count < 3)) {
fileName = st.nextToken();
System.out.println("Downloading " +
fileName + " to C:\\");
try
{
if (ftp.downloadFile(fileName,
"C:\\" + fileName))
{
System.out.println("Download
successful!");
} else {
System.out.println("Error
downloading " + fileName);
}
} catch(Exception de) {
System.out.println("ERROR: " +
de.getMessage());
}
count++;
}
}
} else {
System.out.println("There are no Subdirectories!");
}
ftp.logout();
ftp.disconnect();
System.out.println("Disconnected and Logged Out.");
} else {
System.out.println("Sorry. Could not connect.");
}
} catch(Exception e) {
e.printStackTrace();
try { ftp.disconnect(); } catch(Exception e2) {}
}
}