apache - Java FTPClient listFiles return empty result with unicode path -
i tried list ftp server , have encoding problem
ftpclient.listfiles(string path) method
its returns empty array if path has non-latin characters.
(also using server python , perl scripts in unicode - , there havent problems this)
please solve problem.
this method connection 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 " + host + "."); ftp.connect(host); debug(ftp.getreplystring()); debug("set passive transfer mode"); ftp.enterlocalpassivemode(); debug(ftp.getreplystring()); debug("login " + 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, should check reply code 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 ftp.server.com. 220 ftp server 220 ftp server login ftp.server.com. 230 login successful. set binary transfer mode 200 switching binary mode. set passive transfer mode 200 switching binary mode. buffer size = 1024
here 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, path
i hope answer, resolved issue day myself =) hope useful someone.
solution is:
string encoded = new string(utf8_path.getbytes("utf-8"), "iso-8859-1"); ftpfile[] file_list = ftp.listfiles(encoded); // win!
Comments
Post a Comment