[原] 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 了。嘿嘿嘿嘿。。。
修正后的代码(省略后面没有变化的部分):
- public class UnixListParser implements FTPListParser {
- // bianbian.org: Pattern有问题,修正
- private static final Pattern PATTERN = Pattern
- .compile("^([dlcbsp\\-])[r\\-][w\\-][xsS\\-][r\\-][w\\-][xsS\\-][r\\-][w\\-][xtT\\-]\\s+"
- + "(?:\\d+\\s+)?\\S+\\s*\\S+\\s+(\\d+)\\s+(?:(\\w{3})\\s+(\\d{1,2}))\\s+"
- + "(?:(\\d{4})|(?:(\\d{1,2}):(\\d{1,2})))\\s+"
- + "([^\\\\/*?\"<>|]+)(?: -> ([^\\\\*?\"<>|]+))?$");
- private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
- "MMM dd yyyy HH:mm", Locale.US);
- public FTPFile[] parse(String[] lines) throws FTPListParseException {
- int currentYear = new GregorianCalendar().get(Calendar.YEAR);
- int i, jump = 0, size = lines.length;
- //bianbian.org: glftpd 的第一行和 ls -l 一样,会丢个"total N"过来,先去掉
- if (size > 0 && lines[0].startsWith("total")) {
- size--;
- jump = 1;
- }
- FTPFile[] ret = new FTPFile[size];
- for (i = 0; i < size; i++) {
- Matcher m = PATTERN.matcher(lines[i + jump]);
- if (m.matches()) {
- ret[i] = new FTPFile();
- // Retrieve the data.
- char typeChar = m.group(1).charAt(0);
- String sizeString = m.group(2);
- String monthString = m.group(3);
- String dayString = m.group(4);
- String yearString = m.group(5);
- String hourString = m.group(6);
- String minuteString = m.group(7);
- String nameString = m.group(8);
- String linkedString = m.group(9);
- // Parse the data.
- if ("-cbsp".indexOf(typeChar) > -1) {
- ret[i].setType(FTPFile.TYPE_FILE);
- } else if (typeChar == 'd') {
- ret[i].setType(FTPFile.TYPE_DIRECTORY);
- } else if (typeChar == 'l') {
- ret[i].setType(FTPFile.TYPE_LINK);
- ret[i].setLink(linkedString);
- } else {
- throw new FTPListParseException();
- }
- ...
©2012 便便代码人生. All Rights Reserved.
. 标签: bug, FTP, ftp4j, 解决遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道