看到书上用jbuilder9做了个ftp客户端
我就在eclipse里试验了一下
代码如下
这里只是登陆ftp,然后将文件列表名取回
虽说简单,但是已经把如何进行操作的思想弄清了
就是数据流,远程登陆的数据流,本地数据流,input,output流
还剩下的问题就是如果文件名中有汉字,那么显示出来的是乱码
应该很容易解决,唉基本功不扎实啊
package ftpclient;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
public class FtpApp {
/**
* @param args
*/
public static void main(String[] args) {
Ftp ft = new Ftp();
FtpClient fc = null;
int ch;
StringBuffer buf=new StringBuffer();
fc = ft.connectFtp("192.72.88.167","test","test",21);
System.out.print(fc.welcomeMsg);
try {
fc.binary();
TelnetInputStream t = fc.list();
t.setStickyCRLF(true);
while((ch=t.read())>=0){
if(ch=='\n'){
System.out.print(buf.toString());
buf.setLength(0);
}
else{
//System.out.println(ch);
System.out.print((char)ch);
buf.append((char)ch);
}
}
t.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.print(e.getMessage());
}
}
}
package ftpclient;
import java.io.IOException;
import java.util.ArrayList;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
class Ftp {
private FtpClient ftp=null;
public FtpClient connectFtp(String hostname,String username,String password,int port){
FtpClient ft=null;
try {
ft = new FtpClient(hostname,port);
ft.login(username,password);
}
catch(FtpLoginException e2){
System.out.print("用户名密码错误"+e2.getMessage());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
catch(SecurityException e1){
System.out.print("权限不足"+e1.getMessage());
}
this.ftp = ft;
return ft;
}
}
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3271805
[2005-10-15 14:35:34.0] Java虚拟机的 10 年文/曹晓刚
[2005-10-15 20:09:23.0] JAVA初级小结
[2005-10-19 15:26:53.0] 在DOS环境下进入FTP
[2005-10-19 16:01:13.0] Thinik of java 读书笔记――――――――继承
[2005-10-15 13:18:01.0] Java Diary(10.2--10.5)