public boolean connectServer(String ip, int port, String userName,String userPwd, String path) {
ftpClient = new FTPClient();
try {
// 连接
ftpClient.connect(ip, port);
// 登录
ftpClient.login(userName, userPwd);
ftpClient.enterLocalPassiveMode(); //采用ftp 被动模式 的传输文件协议,这一点很关键
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return false;
}
if (path != null && path.length() > 0) {
// 跳转到指定目录
ftpClient.changeWorkingDirectory(path);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
如上图代码 加了采用被动模式连接就能正常从ftp读取文件了 之前一直报空指针 FTP连接返回状态为221。