Java FTPClient listFiles返回带有Unicode路径的空结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java FTPClient listFiles返回带有Unicode路径的空结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2228字,纯文字阅读大概需要4分钟。
内容图文
![Java FTPClient listFiles返回带有Unicode路径的空结果](/upload/InfoBanner/zyjiaocheng/658/eda5093f2f404ea0a372cc8203d4a56f.jpg)
我试图从ftp服务器获取列表
并有编码问题
FTPClient.listfiles(String path)方法
如果路径包含非拉丁字符,则其始终返回空数组.
(我也使用带有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
解决方法:
我希望有人回答,但是那天我自己解决了这个问题=)
希望对某人有用.
解决方案是:
String encoded = new String(utf8_path.getBytes("UTF-8"), "ISO-8859-1");
FTPFile[] file_list = ftp.listFiles(encoded);
// win!
内容总结
以上是互联网集市为您收集整理的Java FTPClient listFiles返回带有Unicode路径的空结果全部内容,希望文章能够帮你解决Java FTPClient listFiles返回带有Unicode路径的空结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。