FTPClient簡単なサンプル
2008/01/21
commons
ftp
java
使っているのは、commonsのnetです。
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class TestFtpClient {
public static void main(String[] args) throws Exception {
FTPClient client = new FTPClient();
client.setControlEncoding("SJIS");
try {
System.out.println("開始");
client.setDefaultPort(21);
client.connect("localhost"); // FTPサーバーのIPアドレス
client.login("Anonymous", "passowrd"); // FTPサーバーのID,パスワード
boolean isOk = client.changeWorkingDirectory("/");
System.out.println("isOk" + isOk);
FTPFile[] list = client.listFiles();
for (int i = 0; i < list.length; i++) {
String name = list[i].getName();
String group = list[i].getGroup();
System.out.println("group : " + group);
System.out.println("name : " + name);
}
} catch (SocketException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
if (client != null && client.isConnected()) {
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("終了");
}
}
}
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class TestFtpClient {
public static void main(String[] args) throws Exception {
FTPClient client = new FTPClient();
client.setControlEncoding("SJIS");
try {
System.out.println("開始");
client.setDefaultPort(21);
client.connect("localhost"); // FTPサーバーのIPアドレス
client.login("Anonymous", "passowrd"); // FTPサーバーのID,パスワード
boolean isOk = client.changeWorkingDirectory("/");
System.out.println("isOk" + isOk);
FTPFile[] list = client.listFiles();
for (int i = 0; i < list.length; i++) {
String name = list[i].getName();
String group = list[i].getGroup();
System.out.println("group : " + group);
System.out.println("name : " + name);
}
} catch (SocketException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
if (client != null && client.isConnected()) {
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("終了");
}
}
}
: