我試圖讓從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
我面臨同樣的問題。請你幫我.. – Naveen
請在這裏投票https://issues.apache.org/jira/browse/NET-553 – lisak