Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
来源:互联网 发布:手机淘宝还用装旺信吗 编辑:程序博客网 时间:2024/04/28 21:17
Java ftp实现文件的上传和下载
ftp,sftp
运行后发现很长时间没有反应,很久以后抛出如下异常:sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
但是在cmd中ftp是可以通过的,用SecureCRT也是可以连接的,查看SecureCRT配置,看到端口是22.
用jdk自带包实现代码如下:
publicclass FtpTest {
private FtpClientftpclient;
publicvoid connectServer(String ip,int port, String username,
String password, String path) {
try {
if (null == ip) {
thrownew Exception("The service's ip is null!");
}
ftpclient =new FtpClient();
//当端口号为0时,采用默认端口号
if(port == 0){
ftpclient.openServer(ip);
}else{
ftpclient.openServer(ip, port);
}
//ftpclient = new FtpClient(ip, port);
System.out.println(ftpclient.getFtpProxyPort());
ftpclient.login(username, password);
System.out.println("12324");
ftpclient.binary();
if (null != path) {
ftpclient.cd(path);// 跳转到服务器的指定目录
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicvoid connectServer(String ip, String username,
String password, String path){
connectServer(ip,0,username,password,path);
}
publicvoid closeServer() {
if (null !=ftpclient) {
try {
ftpclient.closeServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publicvoid ftpupload(String localfilanme, String remotefilename) {
FileInputStream input =null;
TelnetOutputStream out =null;
try {
if (null == localfilanme ||null == remotefilename) {
thrownew Exception("文件名为空!");
}
File localfile =new File(localfilanme);
if (!localfile.exists() || !localfile.isFile()
|| !localfile.canRead()) {
thrownew Exception("文件不存在或者不是文件!");
}
input =new FileInputStream(localfile);
out =ftpclient.put(remotefilename);
int c = 0;
byte[] bye =newbyte[1024];
while ((c = input.read(bye)) != -1) {
out.write(bye, 0, c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != input) {
input.close();
}
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
publicvoid ftpdownload(String localfilename, String remotefilename) {
FileOutputStream out =null;
TelnetInputStream input =null;
try {
if (null == localfilename ||null == remotefilename) {
thrownew Exception("文件名为空!");
}
File localfile =new File(localfilename);
out =new FileOutputStream(localfile);
input =ftpclient.get(remotefilename);
int c = 0;
byte[] bye =newbyte[1024];
while ((c = input.read(bye)) != -1) {
out.write(bye, 0, c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != input) {
input.close();
}
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
publicstaticvoid main(String[] args) {
FtpTest test =new FtpTest();
String ip ="xxx";
String username ="admin";
String password ="admin";
String path ="/usr/admin/test";
test.connectServer(ip,22,username, password, path);
System.out.println(test.ftpclient.getFtpProxyPort());
test.ftpupload("C:\\Users\\admin\\Desktop\\123.txt","123.txt");
test.ftpdownload("D:\\124.txt", "123.txt");
test.closeServer();
}
}
网上找了很久,没找到什么资料。通过查询ftp资料发,有这样一句解释:FTP服务一般运行在20和21两个端口。端口20用于传输控制流,而端口21用于在客户端和服务器之间传输数据流。再看SecureCRT,发现有一个sftp标签页配置,查询sftp资料,解释如下:sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。发现端口22是 sftp的默认端口,所以,将代码中的端口22改成21即可解决这个问题。
在http://www.dexcoder.com/selfly/article/4028中,页讲解了用jsch包sftp的方式解决。
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- sun.net.ftp.FtpClient下载、上传文件
- Java实现FTP与SFTP文件上传
- ftp实现文件的上传和下载
- commons net jar包实现文件ftp上传和下载功能 (commons net ftp 使用1)
- java实现ftp的上传和下载
- Java 实现ftp文件的上传和下载
- ftp上传和下载文件的java实现
- ftp上传和下载文件的java实现
- ftp上传和下载文件的java实现
- java实现ftp的文件上传和下载
- FTP下载和上传文件的java实现
- Java连接FTP并且实现文件的上传和下载
- java通过apache ftp实现ftp上传文件,下载文件
- Ftp和Sftp上传、下载、删除方法
- java 实现FTP sftp
- JAVA Ftp实现文件上传于下载
- Java实现FTP文件上传与下载
- 网页上传文件,到服务器,再讲数据导入数据库
- Objective-C Block的实现
- 广点通sdk接入 _Banner广告
- 匿名内部类如何同时调用多个方法
- GSP序列模式分析算法
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- iOS xib文件引入的两种方式
- Tips for DDIC and Search Help.docx
- Hiredis-redis cplusplus--redis3M
- 决策分类树算法之ID3,C4.5算法系列
- C 语言中的左值和右值。以及对比数组名和指针取数组元素的区别
- 深入理解Android之Gradle
- mybatis之map.xml文件的解读
- MongoDB is web scale