1. 程式人生 > >根據路徑獲取系統中的檔案,FtpClient所走過的彎路和直接用File獲取。

根據路徑獲取系統中的檔案,FtpClient所走過的彎路和直接用File獲取。

之前客戶現場遇到一個棘手的問題,給定路徑,利用FtpClient獲取裡面xml檔案的時候,返回空,網上有很多解決辦法,大多數是對ftp中文環境,和getFiles()方法裡面的正則表示式進行修改,也嘗試了用一些網上提供的類,但都沒有作用,由於客戶內網環境封鎖的太嚴,遠端不到裡面,所以只好放棄這條方案,改用File直接獲取xml檔案並解析。下面粘出FtpClient 和File程式碼。

package cn.com.wechat.ftp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.SAXException;

public class TtpTest {
    public static void main(String[] args) {
    FTPClient ftpClient = new FTPClient();
    String ftpPath = null;
    String path = "";
    int port = 21;
    String user = "";
    String password = "";
    try {
        // 連線
        ftpClient.connect(path,port);
        // 登入
        ftpClient.login(user, password);
        ftpClient.setDataTimeout(60000); // 設定傳輸超時時間為60秒
        ftpClient.setConnectTimeout(60000); // 連線超時為60秒
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return;
        }
        if (path != null && path.length() > 0) {
            ftpClient.changeWorkingDirectory(ftpPath);
            FTPFile[] ftpFiles = null;
            ftpClient.enterLocalPassiveMode();
            ftpClient.configure(new FTPClientConfig("cn.com.wechat.ftp.UnixFTPEntryParser")); //這裡記得改成你放的位置
            ftpFiles = ftpClient.listFiles();
            for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
                FTPFile file = ftpFiles[i];
                // 傳送簡訊xml檔案
                if (!file.isFile()) {
                    continue;
                }
                InputStream in = ftpClient.retrieveFileStream(file.getName());
                ftpClient.getReply();
                SAXBuilder builder = new SAXBuilder();
                Document document = builder.build(in);// 獲得文件物件
                Element root = document.getRootElement();// 獲得根節點
                List<Element> list = root.getChildren();
                for (Element e : list) {
                	String name1 = e.getChildText("name");
                }
                ftpClient.deleteFile(file.getName());// 刪除ftp上的檔案   
                in.close();
            }
        }
        ftpClient.logout();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

利用到UnixFTPEntryParser和FTPTimestampParserImplExZH這兩個java類,解決正則表示式問題,我在下面也粘出來

package cn.com.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;
import org.apache.log4j.Logger;

/**
 * 注:common-net-1.4.1.jar原始碼,修改對於日期中文格式的支援,從而解決FTPClient.listFiles()返回為空問題
 * 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
{
	private static Logger logger = Logger.getLogger(UnixFTPEntryParser.class);
    /**
     * 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+"
        + "(\\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日(沒有空格分開)
        //故無法匹配而報錯
        //將下面字串改為:
        + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S+\\s+\\S+)|(?:\\S+))\\s+"
        //+ "((?:\\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+":修改日期重置為當前時間");
            	file.setTimestamp(Calendar.getInstance());
            }
            
            
            // bcdlfmpSs-
            switch (typeStr.charAt(0))
            {
            case 'd':
                type = FTPFile.DIRECTORY_TYPE;
                break;
            case 'l':
                type = FTPFile.SYMBOLIC_LINK_TYPE;
                break;
            case 'b':
            case 'c':
                isDevice = true;
                // break; - fall through
            case 'f':
            case '-':
            	type = FTPFile.FILE_TYPE;
            	break;
            default:
                type = FTPFile.UNKNOWN_TYPE;
            }

            file.setType(type);

            int g = 4;
            for (int access = 0; access < 3; access++, g += 4)
            {
                // Use != '-' to avoid having to check for suid and sticky bits
                file.setPermission(access, FTPFile.READ_PERMISSION,
                                   (!group(g).equals("-")));
                file.setPermission(access, FTPFile.WRITE_PERMISSION,
                                   (!group(g + 1).equals("-")));

                String execPerm = group(g + 2);
                if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0)))
                {
                    file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true);
                }
                else
                {
                    file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
                }
            }

            if (!isDevice)
            {
                try
                {
                    file.setHardLinkCount(Integer.parseInt(hardLinkCount));
                }
                catch (NumberFormatException e)
                {
                    // intentionally do nothing
                }
            }

            file.setUser(usr);
            file.setGroup(grp);

            try
            {
                file.setSize(Long.parseLong(filesize));
            }
            catch (NumberFormatException e)
            {
                // intentionally do nothing
            }
            
            if (null == endtoken)
            {
                file.setName(name);
            }
            else
            {
                // oddball cases like symbolic links, file names
                // with spaces in them.
                name += endtoken;
                if (type == FTPFile.SYMBOLIC_LINK_TYPE)
                {

                    int end = name.indexOf(" -> ");
                    // Give up if no link indicator is present
                    if (end == -1)
                    {
                        file.setName(name);
                    }
                    else
                    {
                        file.setName(name.substring(0, end));
                        file.setLink(name.substring(end + 4));
                    }

                }
                else
                {
                    file.setName(name);
                }
            }
            return file;
        } else {
        	logger.info("matches(entry) failure:"+entry);
        }
        return null;
	}

    /**
     * Defines a default configuration to be used when this class is
     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
     * parameter being specified.
     * @return the default configuration for this parser.
     */
    protected FTPClientConfig getDefaultConfiguration() {
        return new FTPClientConfig(
                FTPClientConfig.SYST_UNIX,
                DEFAULT_DATE_FORMAT,
                DEFAULT_RECENT_DATE_FORMAT,
                null, null, null);
    }

}

package cn.com.wechat.ftp;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.net.ftp.parser.FTPTimestampParserImpl;

/**
 * @desc: 此類的原始貢獻者為hzwei206, 
 *        解決apache ftp中文語言環境下,
 *        FTPClient.listFiles()為空的bug
 * @author<
[email protected]
> * @since 2015-7-27 */ public class FTPTimestampParserImplExZH extends FTPTimestampParserImpl { private SimpleDateFormat defaultDateFormat = new SimpleDateFormat("mm d hh:mm"); private SimpleDateFormat recentDateFormat = new SimpleDateFormat("yyyy mm d"); /** * @author hzwei206 將中文環境的時間格式進行轉換 */ private String formatDate_Zh2En(String timeStrZh) { if (timeStrZh == null) { return ""; } int len = timeStrZh.length(); StringBuffer sb = new StringBuffer(len); char ch = ' '; for (int i = 0; i < len; i++) { ch = timeStrZh.charAt(i); if ((ch >= '0' && ch <= '9') || ch == ' ' || ch == ':') { sb.append(ch); } } return sb.toString(); } /** * Implements the one {@link FTPTimestampParser#parseTimestamp(String) method} in the {@link FTPTimestampParser * FTPTimestampParser} interface according to this algorithm: If the recentDateFormat member has been defined, try * to parse the supplied string with that. If that parse fails, or if the recentDateFormat member has not been * defined, attempt to parse with the defaultDateFormat member. If that fails, throw a ParseException. * * @see org.apache.commons.net.ftp.parser.FTPTimestampParser#parseTimestamp(java.lang.String) */ public Calendar parseTimestamp(String timestampStr) throws ParseException { timestampStr = formatDate_Zh2En(timestampStr); Calendar now = Calendar.getInstance(); now.setTimeZone(this.getServerTimeZone()); Calendar working = Calendar.getInstance(); working.setTimeZone(this.getServerTimeZone()); ParsePosition pp = new ParsePosition(0); Date parsed = null; if (this.recentDateFormat != null) { parsed = recentDateFormat.parse(timestampStr, pp); } if (parsed != null && pp.getIndex() == timestampStr.length()) { working.setTime(parsed); working.set(Calendar.YEAR, now.get(Calendar.YEAR)); if (working.after(now)) { working.add(Calendar.YEAR, -1); } } else { pp = new ParsePosition(0); parsed = defaultDateFormat.parse(timestampStr, pp); // note, length checks are mandatory for us since // SimpleDateFormat methods will succeed if less than // full string is matched. They will also accept, // despite "leniency" setting, a two-digit number as // a valid year (e.g. 22:04 will parse as 22 A.D.) // so could mistakenly confuse an hour with a year, // if we don't insist on full length parsing. if (parsed != null && pp.getIndex() == timestampStr.length()) { working.setTime(parsed); } else { throw new ParseException("Timestamp could not be parsed with older or recent DateFormat", pp.getIndex()); } } return working; } }

上面的程式碼沒有解決我的問題,我再粘出我的解決辦法


	   List<String> ids = new ArrayList<String>();
	   String path = "d:/";
   	    File file = new File(path);
       File[] tempList = file.listFiles();

       for (int i = 0; i < tempList.length; i++) {
           if (tempList[i].isDirectory()) {
               continue;
           }
           if (tempList[i].isFile()) {
               File f = new File(tempList[i].toString());
					org.w3c.dom.Document document;
					try {
						DocumentBuilderFactory documentBuilderFactoryImpl = DocumentBuilderFactory.newInstance();
						DocumentBuilder documentBuilder = documentBuilderFactoryImpl.newDocumentBuilder();
						document = documentBuilder.parse(f);
//						document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
						org.w3c.dom.Element element = document.getDocumentElement();
						String SmsInner_Id = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
						f.delete();
						ids.add(SmsInner_Id);
					} catch (SAXException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ParserConfigurationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
           }
       }
    

好了,ftp獲取不到就不用ftp了。