项目中需要从远程获取文件导入到数据库,供应方给了一个获取地址,很肯定的说是ftp协议。

刚开始想着减少依赖,使用JDK自带的ftp客户端sun.net.ftp.FtpClient,连接代码如下:

FtpClient ftpClient = new FtpClient();
ftpClient.openServer(FTP_IP, FTP_PORT);
ftpClient.login(LOGIN_NAME, PASSWORD);
ftpClient.binary();
TelnetInputStream is = ftpClient.get("/ftp/re/20140713.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

但是一运行等了老半天后,返回如下错误:

Exception in thread "main" sun.net.ftp.FtpProtocolException: Welcome message: SSH-2.0-OpenSSH_6.2

at sun.net.ftp.FtpClient.openServer(FtpClient.java:490)

at ejbModule.servlet.schedul.ImportMarkServlet.doPost(ImportMarkServlet.java:62)

at ejbModule.servlet.schedul.ImportMarkServlet.main(ImportMarkServlet.java:87)

网上查了下,基本无相关资料,不想太过折腾,换成了apache的commons-net,连接代码如下:

FtpClient ftpClient = new FtpClient();
ftpClient.connect(FTP_IP, FTP_PORT);
ftpClient.login(LOGIN_NAME, PASSWORD);
// 中文支持
ftpClient.setControlEncoding("UTF-8");   
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
ftpClient.enterLocalPassiveMode();  
  InputStream is = ftpClient.retrieveFileStream("/ftp/re/20140713.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

这次不用等老半天了,结果马上就返回,可是还是出现异常:

Exception in thread "main" org.apache.commons.net.MalformedServerReplyException: Could not parse response code.

Server Reply: SSH-2.0-OpenSSH_6.2

at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:337)

at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)

at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:400)

at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:924)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:183)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)

at ejbModule.servlet.schedul.ImportMarkServlet.doPost(ImportMarkServlet.java:57)

at ejbModule.servlet.schedul.ImportMarkServlet.main(ImportMarkServlet.java:89)

网上查了下,资料又是基本没有,搞了老半天终于看到一位哥们的话给了我灵感:是不是连接的协议错了?

连接的协议,我用FTP的客户端工具FileZilla连接是可以成功的,所以一开始就忽略了这方面的问题,经他这么一提醒,去仔细的看了下,

果然,FileZilla连接的host上,是以sftp://开头的,看来这是sftp协议无疑,ftp和sftp还是有本质区别的。知道了原因,换个jar包重新编码,这里使用了jsch-0.1.51.jar,主要连接代码如下:

ChannelSftp sftp = null;
JSch jsch = new JSch();
Session sshSession = jsch.getSession(LOGIN_NAME, FTP_IP, FTP_PORT);
sshSession.setPassword(PASSWORD);
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
InputStream is = sftp.get("/ftp/re/20140713.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

这次一下成功,因为这个问题折腾老半天,真是太不值了,忍不住想说脏话。不过要是我一开始就细心点的话也能避免,唉!

你可能感兴趣的内容
Linux FTP 服务器配置说明 收藏,5199 浏览
FTP服务器日志解析 收藏,8538 浏览
linux下NFS、FTP使用讲解 收藏,2466 浏览
1条评论
12345 1年前
工作几年了??

selfly

交流QQ群:32261424
Owner