package cn.com.asb.wechat.ftp;
/*
* Copyright 2001-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.ParseException;
import java.util.Calendar;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl;
/**
* 注:common-net-1.4.1.jar源码,修改对于日期中文格式的支持,
* 从而解决因中文设置而造成的FTPClient.listFiles()返回为空问题。
*
* Modify By Sun Ying. Fix the problem that the original regular expression can not match "." .
* This problem can also cause FTPClient.listFiles() return null.
*
* Implementation FTPFileEntryParser and FTPFileListParser for standard
* Unix Systems.
*
* This class is based on the logic of Daniel Savarese's
* DefaultFTPListParser, but adapted to use regular expressions and to fit the
* new FTPFileEntryParser interface.
* @version $Id: UnixFTPEntryParser.java 161712 2005-04-18 02:57:04Z scohen $
* @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
*/
public class UnixFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
{
/**
* months abbreviations looked for by this parser. Also used
* to determine which month is matched by the parser
*/
private static final String DEFAULT_MONTHS =
"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
static final String DEFAULT_DATE_FORMAT
= "MMM d yyyy"; //Nov 9 2001
static final String DEFAULT_RECENT_DATE_FORMAT
= "MMM d HH:mm"; //Nov 9 20:06
static final String NUMERIC_DATE_FORMAT
= "yyyy-MM-dd HH:mm"; //2001-11-09 20:06
/**
* Some Linux distributions are now shipping an FTP server which formats
* file listing dates in an all-numeric format:
* <code>"yyyy-MM-dd HH:mm</code>.
* This is a very welcome development, and hopefully it will soon become
* the standard. However, since it is so new, for now, and possibly
* forever, we merely accomodate it, but do not make it the default.
* <p>
* For now end users may specify this format only via
* <code>UnixFTPEntryParser(FTPClientConfig)</code>.
* Steve Cohen - 2005-04-17
*/
public static final FTPClientConfig NUMERIC_DATE_CONFIG =
new FTPClientConfig(
FTPClientConfig.SYST_UNIX,
NUMERIC_DATE_FORMAT,
null, null, null, null);
/**
* this is the regular expression used by this parser.
*
* Permissions:
* r the file is readable
* w the file is writable
* x the file is executable
* - the indicated permission is not granted
* L mandatory locking occurs during access (the set-group-ID bit is
* on and the group execution bit is off)
* s the set-user-ID or set-group-ID bit is on, and the corresponding
* user or group execution bit is also on
* S undefined bit-state (the set-user-ID bit is on and the user
* execution bit is off)
* t the 1000 (octal) bit, or sticky bit, is on [see chmod(1)], and
* execution is on
* T the 1000 bit is turned on, and execution is off (undefined bit-
* state)
*/
private static final String REGEX =
"([bcdlfmpSs-])"
//+"(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+"
//To match the "." of "-rw-rw-rw-." @author Sun Ying
+"(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\.*\\s+"
+ "(\\d+)\\s+"
+ "(\\S+)\\s+"
+ "(?:(\\S+)\\s+)?"
+ "(\\d+)\\s+"
/*
numeric or standard format date
*/
//中文匹配问题出在此处,这个匹配只匹配2中形式:
//(1)2008-08-03
//(2)(Jan 9) 或 (4月 26)
//而出错的hp机器下的显示为 8月20日(没有空格分开)
//故无法匹配而报错
//将下面字符串改为:
+ "((?:\\S+\\s+\\S+)|(?:\\S+))\\s+"
//+ "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S+\\s+\\S+)|(?:\\S+))\\s+"
//上面这句是别人改的,但是第三部分(?:\\S+)已经包含了第一部分(?:\\d+[-/]\\d+[-/]\\d+),
//所以我去掉了第一部分. @author Sun Ying
//+ "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S+\\s+\\S+))\\s+"
/*
year (for non-recent standard format)
or time (for numeric or recent standard format
*/
+ "(\\d+(?::\\d+)?)\\s+"
+ "(\\S*)(\\s*.*)";
/**
* The default constructor for a UnixFTPEntryParser object.
*
* @exception IllegalArgumentException
* Thrown if the regular expression is unparseable. Should not be seen
* under normal conditions. It it is seen, this is a sign that
* <code>REGEX</code> is not a valid regular expression.
*/
public UnixFTPEntryParser()
{
this(null);
}
/**
* This constructor allows the creation of a UnixFTPEntryParser object with
* something other than the default configuration.
*
* @param config The {@link FTPClientConfig configuration} object used to
* configure this parser.
* @exception IllegalArgumentException
* Thrown if the regular expression is unparseable. Should not be seen
* under normal conditions. It it is seen, this is a sign that
* <code>REGEX</code> is not a valid regular expression.
* @since 1.4
*/
public UnixFTPEntryParser(FTPClientConfig config)
{
super(REGEX);
configure(config);
}
/**
* Parses a line of a unix (standard) FTP server file listing and converts
* it into a usable format in the form of an <code> FTPFile </code>
* instance. If the file listing line doesn't describe a file,
* <code> null </code> is returned, otherwise a <code> FTPFile </code>
* instance representing the files in the directory is returned.
* <p>
* @param entry A line of text from the file listing
* @return An FTPFile instance corresponding to the supplied entry
*/
public FTPFile parseFTPEntry(String entry) {
FTPFile file = new FTPFile();
file.setRawListing(entry);
int type;
boolean isDevice = false;
if (matches(entry))
{
String typeStr = group(1);
String hardLinkCount = group(15);
String usr = group(16);
String grp = group(17);
String filesize = group(18);
String datestr = group(19) + " " + group(20);
String name = group(21);
String endtoken = group(22);
try
{
//file.setTimestamp(super.parseTimestamp(datestr));
FTPTimestampParserImplExZH Zh2En = new FTPTimestampParserImplExZH();
file.setTimestamp(Zh2En.parseTimestamp(datestr));
}
catch (ParseException e)
{
//logger.error(e, e);
//return null; // this is a parsing failure too.
//logger.info(entry+":修改日期重置为当前时间");
- 1
- 2
- 3
前往页