Java FTPClient listFiles返回带有unicode路径的空结果

标签 java apache unicode ftp

我试图从 ftp 服务器获取列表 并且有编码问题

FTPClient.listfiles(String path) 方法

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

(我也在 unicode 中使用带有 python 和 perl 脚本的服务器 - 而且没有这样的问题)

请帮助解决这个问题。

这种连接调试输出的方法:

  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 

最佳答案

我希望有人回答,但我那天自己解决了这个问题 =) 希望对某人有用。

解决办法是:

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

    // win!

关于Java FTPClient listFiles返回带有unicode路径的空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16668518/

相关文章:

Java - 使用哈希和数组匹配比较两个文件

java - camera2 停止使用 CameraDevice.StateCallBack onError 值 4

php - 为大型公司在 phpmyadmin 中创建大型 Web 应用程序

jquery - 如何在 Apache Wicket 应用程序中向 jQuery TreeTable 添加和展开全部/折叠全部?

java - maven-surefire-plugin 虚拟 :dummy. jar :1. 0 丢失

java - 随机访问文件内存泄漏

apache - 将 apache documentRoot 设置为 symlink(便于部署)

php - 如何将带有 PHP PDO 的 UCS-2 数据插入 MySQL?

java - 如何从 Java 中的字符串中删除\u200B(零长度空白 Unicode 字符)?

html - 使用带有 utf 8 的网络字体的 firefox 和 IE 中的特殊字符问题