FTPClient 的changeWorkingDirectory方法设置路径后放回false不知道是为什么?

jiachaochao309 2015-11-09 03:58:00
public FTPFile[] listDataFiles(String datapath) {
FTPClient ftp = new FTPClient();
FTPFile[] files=null;
try {
loadFtpConfig();
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

ftp.connect(ftpHost, ftpPort);

// 下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件


// ftp.changeWorkingDirectory("/home");
ftp.setControlEncoding("UTF-8");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
// 登录ftp
ftp.login(ftpUser, ftpPwd);
// 看返回的值是不是230,如果是,表示登陆成功
reply = ftp.getReplyCode();
// 以2开头的返回值就会为真
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("连接服务器失败");

return null;
}
System.out.println("登陆服务器成功");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftp.enterRemotePassiveMode();
boolean isChanhge=ftp.changeWorkingDirectory(datapath);
files=ftp.listFiles();
// files=ftp.listFiles("/home/ufida");
ftp.logout();
} catch (BusinessException e) {

e.printStackTrace();
}catch (SocketException e) {
Logger.error(ftpHost+"连接失败!");
}catch (IOException e) {

}

return files;
}
...全文
7356 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_21391877 2019-11-20
  • 打赏
  • 举报
回复
引用 2 楼 陈_小哥哥 的回复:
以“/” 开头的为绝对路径,不以“/”开头的是相对路径,changeWorkingDirectory 会导致访问ftp 服务器的地址不一样
一下被点醒
陈_小哥哥 2019-02-18
  • 打赏
  • 举报
回复
以“/” 开头的为绝对路径,不以“/”开头的是相对路径,changeWorkingDirectory 会导致访问ftp 服务器的地址不一样
wula0010 2015-11-12
  • 打赏
  • 举报
回复
这个要看你的服务器的编码,我的是这样的:

chgDir=ftp.changeWorkingDirectory(new String(remotePath.getBytes(),"ISO-8859-1"));
if (chgDir) {
    ... ...
}

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧