使用 Java FTP client,看看实例代码,很简单的:FTPClient ftpClient = new FTPClient(); ftpClient.connect("ftp.foo.com", "user01", "pass1234"); ftpClient.download("C:\\Temp\\", "README.txt"); // Eventually other operations here ... ftpClient.disconnect();另外还有其他的类库,都挺简单的。
ftpClient.connect("ftp.foo.com", "user01", "pass1234");
ftpClient.download("C:\\Temp\\", "README.txt");
// Eventually other operations here ...
ftpClient.disconnect();另外还有其他的类库,都挺简单的。
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;public class MyFTPClient {
private FtpClient ftpClient = new FtpClient();;
private String host;
private String loginName;
private String loginPassword;
public MyFTPClient(String host,String loginName,String loginPassword) {
this.host = host;
this.loginName = loginName;
this.loginPassword = loginPassword;
}
public void connect() throws IOException {
ftpClient.openServer(host);
ftpClient.login(loginName, loginPassword);
//ftpClient.ascii();
}
public void disconnect() {
try {
ftpClient.closeServer();
ftpClient = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return File[] 获取指定目录下的所有文件,并返回一个数组
* @throws IOException
*/
public File[] listFiles() throws IOException {
ArrayList list = new ArrayList();
TelnetInputStream instream = ftpClient.nameList("/");
BufferedReader reader = new BufferedReader(new InputStreamReader(instream,"utf-8"));//设置编码方式,解决中文乱码问题
String temp = "";
while( (temp=reader.readLine())!= null) {
list.add(temp);
}
File[] files = new File[list.size()];
for(int i=0; i < list.size(); i++) {
files[i] = new File(list.get(i).toString());
}
return files;
}
public static void main(String[] args) throws Exception {
MyFTPClient ftp = new MyFTPClient("192.168.0.13","admin","admin");
ftp.connect(); //连接
File[] files =ftp.listFiles();//获得文件列表
ftp.disconnect();//断开连接
System.out.println("文件个数:"+files.length);
}
}