1. 程式人生 > >又遇到Apache FTPClient下載檔案取不到的問題,趕緊記錄一下解決方法

又遇到Apache FTPClient下載檔案取不到的問題,趕緊記錄一下解決方法

private static String encoding = System.getProperty("file.encoding");
...
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
...
ftpClientInFunction.retrieveFile(new String(fInFunction.getName().getBytes("GBK"),"iso-8859-1"), is);

1、編碼問題

在FTP協議裡面,規定檔名編碼為iso-8859-1,所以目錄名或檔名需要轉碼。

接下來的問題是,我們應該將什麼編碼轉換為此格式。因此,就有了第二種解決方案——把GBK格式的轉換為ISO-8859-1格式。而且,有的人還說,必須得這麼轉。其實,之所以他們能這麼說,我覺得完全是巧合。它的真正原理是,既然FTP協議規定的編碼格式是“ISO-8859-1”,那麼我們確實得將格式轉換一下,然後等伺服器收到檔案時再自動轉換為系統自帶的編碼格式,因此,關鍵不是規定為什麼格式,而是取決於FTP伺服器的編碼格式。因此,如果FTP系統的編碼格式為“GBK”時,第二種方式肯定會成功;但是,如果系統的編碼格式為“UTF-8”時,那就會仍然出現亂碼啦。所以,我們只能通過程式碼先獲取系統的編碼格式,然後通過此編碼格式轉換為ISO-8859-1的編碼格式。獲取方式如下:

private static String encoding = System.getProperty("file.encoding");

2、Apache 自身bug問題

ftpclient listFile方法無法返回正確的資料,一般返回時null ,使用listNames 返回的也是隻有檔名,這個是ftpclient 工具包的一個bug,

轉門有人給出瞭解決的程式碼,需要加入兩個類class FTPTimestampParserImplExZH和class UnixFTPEntryParser,這兩個類打包在package com.zznode.tnms.ra.c11n.nj.resource.ftp中。

            /*
             * ftpclient listFile方法無法返回正確的資料,一般返回時null ,使用listNames 返回的也是隻有檔名,這個是ftpclient 工具包的一個bug,
             * 需要用下面的程式碼,並在原始碼中加入:
             * 
             * package com.zznode.tnms.ra.c11n.nj.resource.ftp
             * 
             * class FTPTimestampParserImplExZH
             * 
             * class UnixFTPEntryParser
             */
            
            FTPClientConfig ftpCfg = new FTPClientConfig("com.zznode.tnms.ra.c11n.nj.resource.ftp.UnixFTPEntryParser");

需要加入的程式碼如下:

/**
 * 
 */
package com.zznode.tnms.ra.c11n.nj.resource.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: 此類的原始貢獻瀤?ohzwei206?
 *        解決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"); //原來是hh:mm 12小時制,現在改為HH:mm 24小時制
	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;
	}
}

另一個class:

package com.zznode.tnms.ra.c11n.nj.resource.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憿暿26
        //而出錯的hp機器下的顯示䶿8暿0日(沒有空格分開ﺿ
        //故無法匹配瀦¥錯
        //將下面字串改為ﺿ
        + "((?:\\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);
    }
    
    
    

}


相關推薦

遇到Apache FTPClient下載檔案到的問題趕緊記錄一下解決方法

private static String encoding = System.getProperty("file.encoding"); ... ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(

HDFS原始碼檔案過大IDEA開啟失敗解決方法

問題現象:hadoop 3.1.0原始碼檔案ClientNamenodeProtocolProtos大小4M+,IDEA開啟時載入失敗,ClientNamenodeProtocolPB報錯找不到類。 ----------------------------------------------------

直接讓瀏覽器下載檔案開啟

直接讓客戶端瀏覽器下載已知型別(*.doc)的檔案  ,而不使用關聯程式開啟。         Web開發人員都有過這樣的疑問,如何讓一個檔案,尤其是一個已知型別的檔案(*.doc),傳送到客戶端,直接提示讓瀏覽者下載,而不是用與它相關聯的程式開啟。     以前我們最

ftpclient下載檔案

package com.icfcc.cpk.util.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.Fi

如何在瀏覽器直接下載檔案輸出

function force_download($filename = '', $data = '', $set_mime = FALSE) { if ($filename === '' OR $data === '') { return; } elseif

java 單個上傳檔案, 批量上傳檔案,單個下載,批量打成zip壓縮包下載檔案(如果能接受httpsevletrequest請求的檔案可以使用MultipartFile[] files)

package net.wkang.intelligent_audit.hospitalization.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor

Struts框架上傳下載檔案輔助類簡單實現Struts上傳圖片以及下載

       首先在看這篇文章的前提下,你得會用Struts框架,有一定的基礎瞭解,說白了瞭解怎麼搭建就行了,然後基本就能順利執行本篇文章的Demo,當然這個類不僅僅侷限於圖片上傳下載的,因為是自己用流寫的方法所以可以支援其他檔案上傳下載。

ubuntu中找到標頭檔案term.h和curses.h的解決方法

創建於 2012-05-16 收藏自個人的百度空間 -------------------------------- 當/usr/include中沒有term.h和curses.h時,包含這兩個標頭檔案的程式會如下報錯: term.h: 沒有那個檔案或目錄 curses.h

nginx比apache處理靜態檔案速度快但是nginx處理大量併發的php請求時容易出現502錯誤頻率大概是多少

首先要明確一點的是502是怎麼出現的,為什麼會出現502呢?一般而言,出現502的錯誤是因為php-cgi連線數不夠導致的。舉個例子:php-cgi開10個程序,前端發20個請求,每個請求的指令碼都sleep100s,那麼必然有至多10個請求會出現502錯誤。因此,出現502是因為php程序不夠用了,和ngi

Android Studio引用遠端依賴包時下載了jar包的解決方法

1.修改build.gradle配置為: allprojects { repositories { jcenter() mavenCentral() google() } } 然後clean後重新編譯即可

vb讀取xls檔案開啟excel程序 ado資料庫方法讀取xls

'Read Excel File Using ADO Public Function Read_Excel _          (ByVal sFile _           As String) As ADODB.Recordset       '函式引數    

vs2010無法開啟專案檔案,此安裝支援該專案型別解決方法

今天在用vs2010開啟一個之前做的Web專案時提示:無法開啟專案檔案,此安裝不支援該專案型別解決方法網上查了很多資料,都是說:原因是vs2010需要把mvc升到3,預設的vs2010的mvc是2。vs2010 mvc 3 下載地址:http://www.microsoft.

Eclipse 中編輯XML檔案能進行提示的解決方法

最近因專案需要,使用了struts,在編輯struts-config.xml檔案時,按了提示鍵 (我的為  Alt + /   )後,不能進行提示,只能進行CTRL+C CTRL+V,比較鬱悶。以前也遇

SpringMVC專案JSP到ModelAndView的資料解決

使用maven構建的springmvc專案EL表示式取不到值 不管是用 ${msg} 還是用JSTL的<c:out value="${msg}"/> 都不行 我是用maven自動生成的we

Excel2007開啟逗號分隔的csv檔案自動分列的問題解決方法

經過嘗試,發現這個問題和字元編碼有關係。只有不是UCS-2 Little Endian編碼的逗號分隔的csv檔案雙擊開啟都可以自動分列,但是又發現了亂碼問題,只有ANSI編碼的檔案雙擊開啟既能自動分列又不會有亂碼問題。 統計結果 是否亂碼 是否自動分列 ANSI N Y

[TP5填坑]關於助手函式input一不小心到get值的解決辦法

宣告:適用於懶人 常規對於這個什麼鬼取不到值我也是很絕望的,通常都是看一遍手冊就開始寫,除非是在沒事才跟你認認真真看,所以,我們這類懶人就非常尷尬了,只能動不動查手冊。 tp5也出很久了,本人出問題的版本是5.0,5.1我實在懶得下。。。

IIS7出現未能載入檔案或程式集“XXX.XXX”或它的某一個依賴項。試圖載入格式正確的程式的解決方法

問題:本地web專案執行正常,在本地IIS上面釋出依舊執行正常,釋出到伺服器上的IIS上面出現此錯誤。 解決方法: 將應用程式 生成為  目標平臺:Any CPU; 測試環境是 筆記本 win7 64

關於最近一直糾纏我的c3p0-config.xml配置檔案到的問題終於解決\(^o^)/

從一開始看官方的文件中的說明,關於配置c3p0連線池,如果要使用xml來配置,需要將xml檔案放在classpath的路徑下。所以我就放好了 結果執行後,等待了很久,最後得到一堆紅色的讓人崩潰的error 心態瞬間崩了,喝了一口 雪碧纖維+ (這不是廣告-_-)冷靜一

Android連線伺服器從伺服器獲取資料以及從伺服器下載檔案(單多執行緒)

首先需要在Eclipse中建立一個伺服器,在其中存入要下載的檔案,具體可參考之前的伺服器篇。 ScollView可以上下滑動 另外還有,android中的網路連線與之前java中可以通用,可以參照之前伺服器客戶端通訊篇。 新增的許可權

Eclipse中寫Hibernate的hbm.xml檔案自動提示的問題解決

今天在手動配置Hibernate的hbm.xml檔案時,出現不能自動提示輸入的問題,更詭異的是不同專案中,有的提示,有的不提示。然後按照如下方法進行解決。 一、首先需要有dtd檔案,這個可以從Hibernate自動的project下的例項檔案中獲取,我這裡用的Hiberna