ftp*;
这是一个不被官方支持的但是放在JDK下面的FTP包正因为不被支
持所以没有官方提供API这是其最大的缺陷之一最重要由于不是官方支持的
所以文档也是没有的
l
这里有该包的API
先给一个简单的例子(例子来源互联网)
)显示FTP服务器上的文件
void ftpList_actionPerformed(ActionEvent e) {
String server=serverEditgetText();//输入的FTP服务器的IP地址
String user=userEditgetText(); file://登/录FTP服务器的用户名
String password=passwordEditgetText();//登录FTP服务器的用户名的口令
String path=pathEditgetText();//FTP服务器上的路径
try {
FtpClient ftpClient=new FtpClient();//创建FtpClient对象
ftpClientopenServer(server);//连接FTP服务器
ftpClientlogin(user password);//登录FTP服务器
if (pathlength()!=) ftpClientcd(path);
TelnetInputStream is=ftpClientlist();
int c;
while ((c=isread())!=) {
Systemoutprint((char) c);}
isclose();
ftpClientcloseServer();//退出FTP服务器
} catch (IOException ex) {;}
}
)从FTP服务器上下传一个文件
void getButton_actionPerformed(ActionEvent e) {
String server=serverEditgetText();
String user=userEditgetText();
String password=passwordEditgetText();
String path=pathEditgetText();
String filename=filenameEditgetText();
try {
FtpClient ftpClient=new FtpClient();
ftpClientopenServer(server);
ftpClientlogin(user password);
if (pathlength()!=) ftpClientcd(path);
ftpClientbinary();
TelnetInputStream is=ftpClientget(filename);
File file_out=new File(filename);
FileOutputStream os=new
FileOutputStream(file_out);
byte[] bytes=new byte[];
int c;
while ((c=isread(bytes))!=) {
oswrite(bytesc);
}
isclose();
osclose();
ftpClientcloseServer();
} catch (IOException ex) {;}
}
)向FTP服务器上上传一个文件
void putButton_actionPerformed(ActionEvent e) {
String server=serverEditgetText();
String user=userEditgetText();
String password=passwordEditgetText();
String path=pathEditgetText();
String filename=filenameEditgetText();
try {
FtpClient ftpClient=new FtpClient();
ftpClientopenServer(server);
ftpClientlogin(user password);
if (pathlength()!=) ftpClientcd(path);
ftpClientbinary();
TelnetOutputStream os=ftpClientput(filename);
File file_in=new File(filename);
FileInputStream is=new FileInputStream(file_in);
byte[] bytes=new byte[];
int c;
while ((c=isread(bytes))!=){
oswrite(bytesc);}
isclose();
osclose();
ftpClientcloseServer();
} catch (IOException ex) {;}
}
}
看了这个例子应该就能用他写东西了
这个包缺点很多首先就是不被支持也不被官方推荐使用
其次是这个包功能过于简单简单到无法区分FTP服务器上的File是文件还是目录有人说
通过返回的字符串来判断但是据说FTP在不同系统下返回的东西不大一样所以如果通过
判断字符串会有不好移植的问题
自己想出了一个办法通过FtpClient中的cd方法来判断
代码如下
try{
ftpcd(file);//file为当前判断的文件
//如果过了说明file是目录
}
catch(IOException e){
//说明file是文件
}
finally{
ftpcd();//返回上级目录继续判断下一文件
}
我用这种方法做过尝试结果是只能判断正确一部分有些目录仍会被认做文件不知道
是我的方法有错还是别的什么原因
如果对FTP服务没有过高的要求使用这个包还是不错的因为他本身就包含在JDK中不
存在CLASSPATH的问题不需要导入外部包较为方便
ftp*;
这个包在Jakarta Commons Net library里现在的最高版本是可以从以下地址
下载
net
zip
里面包含了打包好的jarAPI及全部的class文件
net
srczip
这里包含一些例子以及全部的代码
给出一个该包的例子
import ftp*;
public static void getDataFiles( String server
String username
String password
String folder
String destinationFolder
Calendar start
Calendar end )
{
try
{
// Connect and logon to FTP Server
FTPClient ftp = new FTPClient();
nnect( server );
ftplogin( username password );
Systemoutprintln(Connected to +
server + );
Systemoutprint(ftpgetReplyString());
// List the files in the directory
ftpchangeWorkingDirectory( folder );
FTPFile[] files = ftplistFiles();
Systemoutprintln( Number of files in dir: + fileslength );
DateFormat df = DateFormatgetDateInstance( DateFormatSHORT );
for( int i=; i
{
Date fileDate = files[ i ]getTimestamp()getTime();
if( pareTo( startgetTime() ) >= &&
pareTo( endgetTime() ) <= )
{
// Download a file from the FTP Server
Systemoutprint( dfformat( files[ i ]getTimestamp()getTime() ) );
Systemoutprintln( \t + files[ i ]getName() );
File file = new File( destinationFolder +
Fileseparator + files[ i ]getName() );
FileOutputStream fos = new FileOutputStream( file );
ftpretrieveFile( files[ i ]getName() fos );
fosclose();
filesetLastModified( fileDategetTime() );
}
}
// Logout from the FTP Server and disconnect
ftplogout();
ftpdisconnect();
}
catch( Exception e )
{
eprintStackTrace();
}
}
同ftp相同都是先建立FtpClient(注意两包的大小写不同)的实例然后通过
connect()方法连接login()方法登陆但是ftp*明显比sun
netftp功能强大很多
ftp*包将FTP中的file单独出来成为了一个新类FTPFile还有
类FTPFileEntryParserparse没有仔细研究但是从字面来看应该是转化为某种形势的
类有待研究
同时这个commonsnetjar包中也提供了FTP服务器telnetmail等一系列类库
ftp*包的缺点在于需要设置classpath并且需要下载jakarta
orojar这个包才能运行(如果没有这个包会在ftplistFiles()方法后抛出找不
到class异常)此包无须在代码中import只需设置在classpath中即可下载地址
orozip
如果想要强大的FTP服务那么ftp*包应该是你的最好选择而
且也是开源免费的
这个包的问题是:
使用Jakarta Commons Net library需要在环境变量里面编辑classpath
这是不方便的地方
另外IBM也有提供一个ftp包大家有兴趣可以搜索一下