FTPClient 中 FTPClient.changeWorkingDirectory(filePath) 代码一直返回 false

FTP文件下载需要的jar包:

commons-net-2.0.jar  有时可能还需要:jakarta-oro.jar

参考:FTPClient参考文档

这里记录下我碰到的问题:

刚开始我的账号和密码直接都配的是我们公司SSH连接服务器的用户名和密码,只是把端口改成了21,原端口是7979,程序竟然能连接上登录成功。

一共有两个IP,A(124.237.121.6) 和 B(124.237.121.126),连接A的时候一切正常,连接B的时候在切换工作目录的时候一直切不过去,changeWorkingDirectory返回的一直是false。

于是开始查询资料,网上总结了下大约有这几种情况:

1、传入的路径含有中文,需要进行转码操作,我的路径没有中文,排除。下面的代码中我添加了这种情况的解决办法。

2、登录后默认目录不是根目录,如果切换的路径写的是相对路径,在切换目录的时候就会找不到(不以“/”开头的路径是相对路径;以“/”开头的路径是绝对路径),我都用的是绝对路径,所以这种情况排除。

3、账号权限问题,我碰到的问题应该归到这种情况中。

我们服务器上的完整目录大概是这样的:/site/wdxcorder/xxx/xxxx/xxx/......

A和B上的文件都在/site/wdxcorder/autoCompose/下

到这里我已经发现用错账号了,从数据库中找到FTP服务器正确的账号和密码,端口还是21。

这个账号能登陆A和B,虽然根目录也都相同,登陆后都是直接访问的/site/文件夹下的内容,区别在于A上面该账号有权限回退访问/site/的上一级目录,但是B上面该账号却没有权限回退访问到/site/的上一级目录

所以,A上面文件/site/wdxcorder/autoCompose/aaa.jpg可以正常访问到,而B上/site/wdxcorder/autoCompose/bbb.jpg 却不能访问到。

账号权限不能改动,所以只能改目录了,程序里添加了判断,访问B上文件的时候把路径中的/site干掉,ok,问题解决!

FTP下载文件工具类:

 1 package com.wdxc.util;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.OutputStream;
 7 
 8 import org.apache.commons.net.ftp.FTPClient;
 9 import org.apache.commons.net.ftp.FTPClientConfig;
10 import org.apache.commons.net.ftp.FTPFile;
11 import org.apache.commons.net.ftp.FTPReply;
12 import org.apache.log4j.Logger;
13 
14 
15 /**
16  * FTP文件下载
17  * @author wangbo
18  *
19  */
20 public class FTPUtil {
21     
22     private static Logger logger = Logger.getLogger(FTPUtil.class);
23     private static int port = 21;
24     private static String username = "xxxxxx";
25     private static String password = "xxxxxxxxxxxxxxxxxx";
26     
27     public static boolean fileDown(String ip,String fileDir,String saveFTPDir){
28     
29         FTPClient ftp = null; 
30         File file = new File(saveFTPDir);    
31         if(!file.exists() && !file.isDirectory()){//如果文件夹不存在则创建 
32             logger.debug("文件目录不存在,创建。");
33             file.mkdirs();
34         }
35         try{
36             //ftp的数据下载
37             ftp = new FTPClient();
38             ftp.connect(ip, port);   
39             ftp.login(username, password);
40             ftp.setFileType(FTPClient.BINARY_FILE_TYPE);//传输图片的话设置文件类型为二进制
41             //ftp.setControlEncoding("UTF-8");//如果有中文文件名的话需要设置
42             
43             //设置linux环境
44             FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);
45             ftp.configure(conf);
46             
47             //判断是否连接成功
48             int reply = ftp.getReplyCode();
49             if (!FTPReply.isPositiveCompletion(reply)){
50                 ftp.disconnect();
51                 logger.debug("FTP服务无法连接!");
52                 return false;
53             }
54             
55             //设置访问被动模式
56             ftp.setRemoteVerificationEnabled(false);
57             ftp.enterLocalPassiveMode();
58             
59             //切换工作目录到文件所在的目录
60             //boolean dir = ftp.changeWorkingDirectory(new String(fileDir.getBytes(),FTP.DEFAULT_CONTROL_ENCODING));//如果是中文路径需要处理
61             boolean dir = ftp.changeWorkingDirectory(fileDir);
62             if (dir) { 
63                 //检索ftp目录下所有的文件
64                 FTPFile[] fs = ftp.listFiles(); 
65                 for(FTPFile f : fs){
66                       File localFile = new File(saveFTPDir+f.getName());    
67                       OutputStream ios = new FileOutputStream(localFile);     
68                       ftp.retrieveFile(f.getName(), ios);  
69                       ios.close(); 
70                 }
71                 return true;
72             }else {
73                 logger.debug("服务器编码方式可能有问题,请检查!");
74                 return false;
75             }
76         }catch (Exception e){
77             e.printStackTrace();
78             logger.debug("ftp下载文件发生异常!");
79             return false;
80         }finally{
81             if(ftp != null)  try {ftp.disconnect();} catch (IOException ioe) {}  
82         }
83     }
84     
85 }
原文地址:https://www.cnblogs.com/wbxk/p/6693985.html