[原] ftp4j的解析list的bug及解决

上次推荐的ftp4j在解析部分FTP站点的目录list的时候遇到了FTPListParseException(也怪FTP协议没有对LIST格式作出标准)。查看源码发现,主要是两个问题:
1)文件权限不只rwx这三个,附加了s、t(详见http://en.wikipedia.org/wiki/File_system_permissions
2)部分ftpd似乎直接调用的“ls -l”输出目录,第一行是“total xxx”
给作者写信了,说不定下个版本就有Sepcial Thanks to bianbian 了。嘿嘿嘿嘿。。。
修正后的代码(省略后面没有变化的部分):

  1. public class UnixListParser implements FTPListParser {
  2.     // bianbian.org: Pattern有问题,修正
  3.     private static final Pattern PATTERN = Pattern
  4.             .compile("^([dlcbsp\\-])[r\\-][w\\-][xsS\\-][r\\-][w\\-][xsS\\-][r\\-][w\\-][xtT\\-]\\s+"
  5.                     + "(?:\\d+\\s+)?\\S+\\s*\\S+\\s+(\\d+)\\s+(?:(\\w{3})\\s+(\\d{1,2}))\\s+"
  6.                     + "(?:(\\d{4})|(?:(\\d{1,2}):(\\d{1,2})))\\s+"
  7.                     + "([^\\\\/*?\"<>|]+)(?: -> ([^\\\\*?\"<>|]+))?$");
  8.     private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
  9.             "MMM dd yyyy HH:mm", Locale.US);
  10.     public FTPFile[] parse(String[] lines) throws FTPListParseException {
  11.         int currentYear = new GregorianCalendar().get(Calendar.YEAR);
  12.         int i, jump = 0, size = lines.length;
  13.         //bianbian.org: glftpd 的第一行和 ls -l 一样,会丢个"total N"过来,先去掉
  14.         if (size > 0 && lines[0].startsWith("total")) {
  15.             size--;
  16.             jump = 1;
  17.         }
  18.         FTPFile[] ret = new FTPFile[size];
  19.         for (i = 0; i < size; i++) {
  20.             Matcher m = PATTERN.matcher(lines[i + jump]);
  21.             if (m.matches()) {
  22.                 ret[i] = new FTPFile();
  23.                 // Retrieve the data.
  24.                 char typeChar = m.group(1).charAt(0);
  25.                 String sizeString = m.group(2);
  26.                 String monthString = m.group(3);
  27.                 String dayString = m.group(4);
  28.                 String yearString = m.group(5);
  29.                 String hourString = m.group(6);
  30.                 String minuteString = m.group(7);
  31.                 String nameString = m.group(8);
  32.                 String linkedString = m.group(9);
  33.                 // Parse the data.
  34.                 if ("-cbsp".indexOf(typeChar) > -1) {
  35.                     ret[i].setType(FTPFile.TYPE_FILE);
  36.                 } else if (typeChar == 'd') {
  37.                     ret[i].setType(FTPFile.TYPE_DIRECTORY);
  38.                 } else if (typeChar == 'l') {
  39.                     ret[i].setType(FTPFile.TYPE_LINK);
  40.                     ret[i].setLink(linkedString);
  41.                 } else {
  42.                     throw new FTPListParseException();
  43.                 }
  44.     ...

©2012 便便代码人生. All Rights Reserved.

. 标签: bug, FTP, ftp4j, 解决

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

posted @ 2008-06-19 20:09  便便嘘嘘  阅读(387)  评论(0编辑  收藏  举报