存档
毕设中要连接FTP,于是找了一下JAVA连接ftp的方法。发现有两个比较常用的包,一个是JDK自带的sun.net.ftp,功能比较简单。一个是org.apache.commons.net.ftp。这里先用sun.net.ftp写个Demo。
package com.ooobj.test;
import java.io.DataInputStream;
import java.io.IOException;
import sun.net.ftp.FtpClient;
public class FTPTest {
public static void main(String[] args) {
try {
FtpClient fc = new FtpClient("192.168.7.128", 21);
fc.login("ooobj", "123");
System.out.println(fc.pwd());
DataInputStream dis = new DataInputStream(fc.nameList("."));
String s = "";
while ((s = dis.readLine()) != null) {
System.out.println(new String(s.getBytes("ISO-8859-1"),"GBK"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
其中nameList()方法是列出文件,不包括文件夹。list()方法则列出文件和文件夹。sun.net.ftp提供的字符编码是”ISO-8859-1″,需要在读取的时候转换,比较麻烦。
337 次浏览 | 没有评论
五 4th, 2010 | Filed under Java笔记
标签: FTP
最新评论