项目中需要从FTP上下载数据,采用了开源的commons-net包。在实际应用中发现了一个问题,有些服务器上调用ftpClient.listFiles()方法可以返回包含文件名的数组,有些服务器上此方法返回NULL。但是ftpClient.listNames()方法能返回路径中的文件名,ftpClient.delete()方法也能删除文件。
命令行连接FTP,执行ls -l 发现返回数据日期的地方比较奇怪。
引用
-rw-rw-rw- 1 username nobody 145 6月22 16时56 xxxxx.csv
drw-rw-rw- 1 username nobody 145 6月22 2010 bak_dir
失败原因就是这里,commons-net包的FTPListParseEngine负责处理通过socket来获取远程服务器的信息。通过ls –l的信息,解析出文件名,文件时间,文件大小,文件权限,文件创建者。。。。。
public FTPFile[] getFiles() throws IOException {
List tmpResults = new LinkedList();
Iterator iter = this.entries.iterator();
while (iter.hasNext()) {
String entry = (String) iter.next();
// 核心方法
// commons-net默认根据机器信息,按照Unix,WinNT返回解析结果,
// 解析方法通过正则表达式,区分匹配各个字段,不支持中文日期,
FTPFile temp = this.parser.parseFTPEntry(entry);
tmpResults.add(temp);
}
return (FTPFile[]) tmpResults.toArray(new FTPFile[0]);
}
//listFiles方法先初始化FTPListParseEngine,initiateListParsing方法
public FTPFile[] listFiles(String pathname) throws IOException{
String key = null;
FTPListParseEngine engine = initiateListParsing(key, pathname);
return engine.getFiles();
}
public FTPListParseEngine initiateListParsing(String parserKey, String pathname)
throws IOException {
if (__entryParser == null) {
// 传递parserKey的方法deprecated了,推荐使用FTPClientConfig
if (null != parserKey) {
__entryParser = __parserFactory.createFileEntryParser(parserKey);
}
// 设置了FTPClientConfig,按照设置生成ParseEngine
else {
if (null != __configuration) {
__entryParser = __parserFactory.createFileEntryParser(__configuration);
}
// 默认情况下,根据机器信息,自动生成ParseEngine
else {
__entryParser = __parserFactory.createFileEntryParser(getSystemName());
}
}
}
return initiateListParsing(__entryParser, pathname);
}
找到原因后,我们有的放矢,具体解析-rw-rw-rw- 1 username nobody 145 6月22 16时56 xxxxx.csv一行数据
/**
* <pre>
* 解析IBM财务FTP服务器返回的一行信息
* -rw-rw-rw- 1 chnnlftp nobody 145 6月22 16时56 finance_back_info_20100617150652.csv
* 取得文件名,文件时间,文件类型,文件大小,文件所属用户。
*
* 本程序不具有复用性!!
* </pre>
*/
public class MyFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
private Class clazz = MyFTPEntryParser.class;
private Log log = LogFactory.getLog(clazz);
/**
* 解析FTP传回的文件信息
*/
public FTPFile parseFTPEntry(String entry) {
log.debug("开始解析,内容为: " + entry);
FTPFile file = new FTPFile();
file.setRawListing(entry);
String[] temp = entry.split("\\s+");
if (temp.length != 8) {
return null;
}
String fileType = temp[0].substring(0, 1);
if ("d".equals(fileType)) {
file.setType(FTPFile.DIRECTORY_TYPE);
} else {
file.setType(FTPFile.FILE_TYPE);
file.setSize(Integer.valueOf(temp[4]));
}
file.setName(temp[7]);
file.setUser(temp[3]);
Calendar date = Calendar.getInstance();
Date fileDate;
// 返回【6月22 2010】形式的日期
if(temp[6].matches("\\d{4}")){
try {
fileDate = new SimpleDateFormat("yyyyMM月dd")
.parse(temp[6] + temp[5]);
} catch (ParseException e) {
throw new RuntimeException("日期解析出错", e);
}
// 返回【6月22 16时56】形式的日期
} else {
int yyyy = date.get(Calendar.YEAR);
try {
fileDate = new SimpleDateFormat("yyyyMM月ddHH时mm")
.parse(yyyy + temp[5] + temp[6]);
} catch (ParseException e) {
throw new RuntimeException("日期解析出错", e);
}
}
date.setTime(fileDate);
file.setTimestamp(date);
return file;
}
// =====================================================================
// 本类只是特定解析一种FTP,没有考虑到使用正则表达式,匹配解析一类FTP
// 核心方法为parseFTPEntry,以下方法没有实现。
// =====================================================================
public MyFTPEntryParser() {
this("");
}
public MyFTPEntryParser(String regex) {
super("");
}
protected FTPClientConfig getDefaultConfiguration() {
return new FTPClientConfig(clazz.getPackage().getName()
+ clazz.getSimpleName(), "", "", "", "", "");
}
}
// 在调用 ftpClient.listNames()方法前,先调用
ftpClient.configure(new FTPClientConfig(package.MyFTPEntryParser));
// package.MyFTPEntryParser:我们的类的全路径
参考:
http://www.blogjava.net/wodong/archive/2008/08/21/wodong.html
分享到:
相关推荐
使用commons-net-1.4.1.jar,FTPClient连接成功了,但是ftpClient.listFiles(path).length=0 怎么办? 下载本工具,彻底解决问题。 不要增加类,也不要修改任何代码。 不要犹豫了,干掉commons-net-1.4.1.jar包,用这...
赠送jar包:commons-net-3.3.jar; 赠送原API文档:commons-net-3.3-javadoc.jar; 赠送源代码:commons-net-3.3-sources.jar; 赠送Maven依赖信息文件:commons-net-3.3.pom; 包含翻译后的API文档:commons-...
commons-logging-1.0-javadoc.jar, commons-logging-1.0.1-javadoc.jar, commons-logging-1.0.1.jar, commons-logging-1.0.2-javadoc.jar, commons-logging-1.0.2.jar, commons-logging-1.0.3-javadoc.jar, commons-...
commons-net-jar包压缩包中包含commons-net-3.3.jar和commons-net-3.4.jar
赠送jar包:spring-data-commons-2.3.9.RELEASE.jar; 赠送原API文档:spring-data-commons-2.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-commons-2.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
commons-beanutils.jar commons-collections-3.1.jar commons-pool-1.2.jar 集合
commons-pool-1.3.jar+commons-pool.jar;java连接池jar包java连接池;java连接池jar;commons-pool-1.2.jar;commons-pool-1.3.jar+commons-pool.jar;java连接池jar包java连接池;java连接池jar;commons-pool-1.2.jar;...
commons-beanutils-1.8.2.jar,commons-codec-1.4.jar,commons-collections-3.2.1.jar,commons-dbcp-1.2.2.jar,commons-digester-2.0.jar,commons-fileupload-1.2.1.jar,commons-httpclient.jar,commons-io-...
赠送jar包:commons-net-3.6.jar; 赠送原API文档:commons-net-3.6-javadoc.jar; 赠送源代码:commons-net-3.6-sources.jar; 赠送Maven依赖信息文件:commons-net-3.6.pom; 包含翻译后的API文档:commons-...
标签:commons、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
图片上传需要的三个jar包(commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar)
赠送jar包:spring-data-commons-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-commons-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-commons-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
commons-dbcp-1.2.2.jar和commons-pool-1.3.jar包,数据源包
赠送jar包:commons-collections-3.2.2.jar; 赠送原API文档:commons-collections-3.2.2-javadoc.jar; 赠送源代码:commons-collections-3.2.2-sources.jar; 赠送Maven依赖信息文件:commons-collections-3.2.2....
commons-dbcp-1.4.jar和commons-pool-1.5.6.jar
commons-fileupload-1.3.3.jar和commons-io-2.6.jar最新版本
commons-net-ftp-2.0.jar.zip, ftp文件传输依赖包,如果在jdk1.7以上使用,可能相关方法有些改动,需要自行查看api
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
commons-fileupload-1.3.2.jar和commons-io-2.5.jar
用commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar组件,可实现 上传