用java编写读取ftp服务器某一文件夹下面的所有文件名,起初我用FTPClient(包用commons-net-1.4.1.jar),即
try {
FTPClient ftp = new FTPClient();
ftp.connect("192.168.1.174",22);
ftp.login("abc", "111");
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return "success";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {}
}
}
发现报错:org.apache.commons.net.MalformedServerReplyException:Could not parse response code.
Server Reply:SSH-2.0-OpenSSH_5.3
后来网上找了很多资料,说服务器如果是sftp方式,那么不能使用FTPClient连接了,我用FileZilla工具是可以访问的。于是我就尝试用JSch(包用jsch-0.1.51.jar)来实现,即
try {
JSch jsch = new JSch();
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
但是发现报了com.jcraft.jsch.JSchException: Auth fail这个错误,所用的用户名、密码、端口都是与我用FileZilla工具成功连接时是一样的,所以我不知道到底还有什么原因?请指导的大侠指点一下,不甚感激
------解决思路----------------------
顶一下。。。。。