Grey2k
Grey2k

Reputation: 504

Java FTPClient listFiles return empty result with unicode path

I tried to get list from ftp server and have encoding problem with

FTPClient.listfiles(String path) method

its always returns empty array if path has non-latin characters.

(also i using with server with python and perl scripts also in unicode - and there are havent problems like this)

Please help to solve this problem.

This method for connection with debug output:

  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;
}

Here connection output:

  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

Here some examples:

   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 

Upvotes: 4

Views: 8664

Answers (3)

fyts
fyts

Reputation: 503

As pointed out in this question, which leads to this article, the commands setControlEncoding("UTF-8"); and setAutodetectUTF8(true); should be called before connect.

Upvotes: 2

Pedro
Pedro

Reputation: 1

excellent in my case use the following code

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);

Thanks!!

Upvotes: 0

Grey2k
Grey2k

Reputation: 504

i hope that anyone answer, but i resolved this issue that day by myself =) hope that will be useful for someone.

solution is:

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

    // win!

Upvotes: 2

Related Questions