设为首页 收藏本站
查看: 2124|回复: 0

[经验分享] 中文FTP环境下,使用commons-net,FTPClient.listFiles()方法返回null的问题及解决办法

[复制链接]

尚未签到

发表于 2016-6-11 06:16:59 | 显示全部楼层 |阅读模式
项目中需要从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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-228780-1-1.html 上篇帖子: wince下mini2440登陆Telnet和FTP需要密码解决方法 下篇帖子: A Mainframe IDE Powered By Unix Technology [11]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表