关于java网络编程!急 小弟,想用java语言编个在线的ftp程序,但不知道怎么入手,请各位大虾们指点迷津!小弟感激不尽啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 去sun找sun.net包相关sun.net.ftp.FtpClient sun.net.TelnetOutputStream import java.io.*; import java.util.*; import java.net.*; import sun.net.ftp.FtpClient; import sun.net.TelnetOutputStream; public class TestFTP { /** The host name of the FTP server. */ private String host = "somename"; /** The user ID to login to the FTP server. */ private String userID = "user"; /** The password to login to the FTP server. */ private String password = "password"; /** The directory on the FTP server to upload files to. */ private String directory = "filesdir"; /** The name of the file you want to upload. */ private String fileName = "somefile.doc"; public static void main(String[] args) { try { FtpClient ftpClient = new FtpClient(); ftpClient.openServer(host); // connect to FTP server ftpClient.login(userID, password); // login ftpClient.binary(); // set to binary mode transfer ftpClient.cd(directory); // change directory File file = new File(fileName); TelnetOutputStream out = ftpClient.put(file.getName()); FileInputStream in = new FileInputStream(file); int c = 0; while ((c = in.read()) != -1 ) { out.write(c); } in.close(); out.close(); ftpClient.closeServer(); } catch (Exception exception) { exception.printStackTrace(); } } } 楼上您好,非得需要下这个包吗?能用socket编程实现吗? 肯定可以的,只要对FTP的包了解一下会解析就可以了,不过实现起来可能比较复杂,楼主加油- -! 只用socket当然可以,不过要熟悉FTP协议。好好研究一下RFC吧。http://www.faqs.org/rfcs/rfc959.html不过俺要打击一下LZ,FTP协议本身虽然非常简单,但是由于列目录等都是要自己解析,所以非常麻烦。比方说,要列出当前目录所有的文件和目录,由于不像File的list方法可以调用jni,FTP只是tcp/ip流,要解析起来非常麻烦。比方说下面这个就是list命令处理的流,你要从中解析出哪些是文件哪些目录,已经文件的更新时间,权限。特别是更新时间:半年以内Jan 31 12:56,半年以前Aug 02 2007,格式不一样,正则表达式很不好写。还有,不同语言的ftp服务器返回不用:英语Aug;中文八月,日语。drwx------ 3 aaaa bbbb 4096 Aug 02 2007 cccccdrwxr-xr-x 3 aaaa aaaa 256 Mar 19 2007 dddddddrwx------ 2 aaaa aaaa 256 Jan 31 12:56 eeeedrwxr-xr-x 2 aaaa aaaa 256 Aug 26 2005 fff总之,很麻烦。 一样都是要自己解析字符串。用java还简单些。 随便一本java书上都有介绍的啊 Scanner.nextLine()的java.util.NoSuchElementException异常如何解决 java新手,什么情况下发生编译错误,什么情况下发生运行错误! 求助:javax.comm.* 关于JAVA帮助文档 询问一个布局问题(刚学JAVA两天) Toolkit.getDefaultToolkit()怎样使用? 菜鸟提问 getInitParameter为什么得到null 救命啊! 请教:在JB6中怎么样连接数据库!! 请问下面程序里最后一行this.repaint();是什么意思。。this是哪个对象 请教
sun.net.ftp.FtpClient
sun.net.TelnetOutputStream
import java.util.*;
import java.net.*;
import sun.net.ftp.FtpClient;
import sun.net.TelnetOutputStream;
public class TestFTP {
/** The host name of the FTP server. */
private String host = "somename";
/** The user ID to login to the FTP server. */
private String userID = "user";
/** The password to login to the FTP server. */
private String password = "password";
/** The directory on the FTP server to upload files to. */
private String directory = "filesdir";
/** The name of the file you want to upload. */
private String fileName = "somefile.doc";
public static void main(String[] args) {
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(host); // connect to FTP server
ftpClient.login(userID, password); // login
ftpClient.binary(); // set to binary mode transfer
ftpClient.cd(directory); // change directory
File file = new File(fileName);
TelnetOutputStream out = ftpClient.put(file.getName());
FileInputStream in = new FileInputStream(file);
int c = 0;
while ((c = in.read()) != -1 ) {
out.write(c);
}
in.close();
out.close();
ftpClient.closeServer();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
http://www.faqs.org/rfcs/rfc959.html不过俺要打击一下LZ,FTP协议本身虽然非常简单,但是由于列目录等都是要自己解析,所以非常麻烦。
比方说,要列出当前目录所有的文件和目录,由于不像File的list方法可以调用jni,FTP只是tcp/ip流,要解析起来非常麻烦。
比方说下面这个就是list命令处理的流,你要从中解析出哪些是文件哪些目录,已经文件的更新时间,权限。
特别是更新时间:半年以内Jan 31 12:56,半年以前Aug 02 2007,格式不一样,正则表达式很不好写。
还有,不同语言的ftp服务器返回不用:英语Aug;中文八月,日语。drwx------ 3 aaaa bbbb 4096 Aug 02 2007 ccccc
drwxr-xr-x 3 aaaa aaaa 256 Mar 19 2007 dddddd
drwx------ 2 aaaa aaaa 256 Jan 31 12:56 eeee
drwxr-xr-x 2 aaaa aaaa 256 Aug 26 2005 fff总之,很麻烦。