2013-05-21 33 views
2

我试图让从FTP服务器 列表,并与的Java FTPClient listFiles返回空结果使用Unicode路径

FTPClient.listfiles(字符串路径)方法具有编码问题

它总是返回空数组,如果路径具有非拉丁字符。

(我也使用与Python和Perl脚本也以Unicode服务器 - 有这样的问题还没有)

请帮助解决这个问题。

这种方法对于调试输出连接:

public static FTPClient ftpConnect(String host, String login, String password) throws IOException { 

    FTPClient ftp = new FTPClient(); 

    FTPClientConfig config = new FTPClientConfig(); 
    ftp.configure(config); 

    debug(ftp.getReplyString()); 
    debug("Connected to " + host + "."); 

    ftp.connect(host); 
    debug(ftp.getReplyString()); 

    debug("Set passive transfer mode"); 
    ftp.enterLocalPassiveMode(); 
    debug(ftp.getReplyString()); 

    debug("Login to " + host + "."); 

    ftp.login(login, password); 
    debug(ftp.getReplyString()); 

    int reply; 

    ftp.setControlEncoding("UTF-8"); 
    ftp.setAutodetectUTF8(true); 
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 

    debug("Set binary transfer mode"); 
    debug(ftp.getReplyString()); 

    debug("Buffer size = " + ftp.getBufferSize()); 

    // After connection attempt, you should check the reply code to verify 
    // success. 
    reply = ftp.getReplyCode(); 

    if (!FTPReply.isPositiveCompletion(reply)) { 
     ftp.disconnect(); 
     debug("FTP server refused connection."); 
     throw new IOException("FTP server refused connection."); 
    } 

    return ftp; 
} 

这里连接输出:

Connected to ftp.server.com. 
    220 FTP Server 

    220 FTP Server 

    Login to ftp.server.com. 
    230 Login successful. 

    Set binary transfer mode 
    200 Switching to Binary mode. 

    Set passive transfer mode 
    200 Switching to Binary mode. 

    Buffer size = 1024 

这里是一些例子:

String source = "/english_name/Новая_папка12"; // non_latin path 
    String escaped_source = StringEscapeUtils.escapeJava(source); 

    FTPFile[] file_list = ftp.listFiles(escaped_source); // empty 
    file_list = ftp.listFiles(escaped_source + '/');  // empty 
    file_list = ftp.listFiles(source);      // empty 
    file_list = ftp.listFiles('"' + source + '"');   // empty 
    file_list = ftp.listFiles(source + '/');    // empty 
    file_list = ftp.listFiles("/english_name");   // ok, but its another path 

回答

2

我希望任何人的答案,但我解决了这个问题当天由我自己=) 希望对某人有用。

的解决方案是:

String encoded = new String(utf8_path.getBytes("UTF-8"), "ISO-8859-1"); 
    FTPFile[] file_list = ftp.listFiles(encoded); 

    // win! 
+0

我面临同样的问题。请你帮我.. – Naveen

+0

请在这里投票https://issues.apache.org/jira/browse/NET-553 – lisak

0

优异的在我的情况下使用下面的代码

FTPClient ftp = new FTPClient(); 
ftp.setControlEncoding("UTF-8"); 

try { 
is = new ByteArrayInputStream(certificate.getEncoded()); 
msg.append(certificate.toString()); 
//************************************************************************ 
ftp.connect(ip_ftp); 
ftp.login(user_ftp,pass_ftp); 
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 

谢谢!

1

如在this question中指出的那样,其导致this article,应在connect之前调用setControlEncoding("UTF-8");setAutodetectUTF8(true);命令。