Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
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 FtpClient ftpclient;
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相关推荐
- 初学Java Web(7)——文件的上传和下载
文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...
- Java Web之文件的上传及下载
一.文件的上传 1. 简介 > 将一个客户端的本地的文件发送到服务器中保存. > 上传文件是通过流的形式将文件发送给服务器. 2.表单的设置 1.向服务器上传一个文件时,表单要使用post ...
- java实现ftp文件的上传与下载
最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...
- nginx java文件上传_Nginx实现文件的上传和下载
文件的上传只要保证特殊的地址先到达Nginx,然后通过Nginx指定至指定的服务器即可,目前配置是本机.文件的下载的做法就是把本机的当前目录下面的文件给返回回去. server { listen ; ...
- FastDFS:Java客户都实现文件的上传、下载、修改、删除
FastDFS:Java客户都实现文件的上传.下载.修改.删除 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- VB FTP操作类(可上传、下载、创建文件夹等等)
可实现FTP上传下载,建文件夹等功能,从网上找了一个类,对其进行修改和功能补充,正常使用,非常方便. 切记在使用FtpFindFirstFile 函数查找相应的文件或文件夹后,要使用InternetC ...
- 如何搭建FTP服务器实现文件的上传及下载?(亲测有效)
FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP是File Transfer Protoc ...
- FTP服务(文件的上传和下载)
文章目录 一.FTP的作用 二.FTP的端口号 三.FTP:匿名.系统.虚拟账户 四.FTP匿名账户的部署 1.关闭防火墙 2.安装vsftp服务 3.编辑配置文件 4.修改属主和属组,并创建文件 5 ...
- 跨境电商在海关报关中 FTP 文件的上传和下载
海关报关步骤: 1.跨境电商中,海关报关中报关文件的上传和下载通过 FTP 的形式执行. 2.跨境电商中每笔订单都是需要报关,将订单的相关参数生成 xml 格式的文件,g根据海关加密报关需求,将文件加 ...
最新文章
- WSS2.0升级到WSS3.0
- Entity Framework数据库初始化四种策略
- C++17新特性之std::string_view
- js数组遍历和对象遍历
- 打印循环换行_一个案例轻松认识Python 循环语句(for)——10以内的素数
- 分享我的Windows live writer 使用经验
- python中lambda的用法
- MS Office 冷门却实用的技巧及软件安装包分享(文末有福利)
- HDU4530:小Q系列故事——大笨钟
- java 6 基础_java基础(6)
- SIR模型的应用 - Influence maximization in social networks based on TOPSIS(3)
- 第十三届蓝桥杯省赛模拟赛
- 最适合使用外汇跟单软件的人
- 命令行LOGO在线生成网站
- 5.2.1_3 普通心理学(彭冉玲第四版) - 第2章 心理的神经生理机制
- A1008 Elevator (20 分)
- 在WindowXP中显示找不到服务器或者DNS错误
- 洛谷 p2387 [noi2014] 膜fa♂森林 lct维护最小生成树
- 刷机总是出现missed hello_金立-GN3002L 无法开机,怎么能解锁刷机呢?
- html2canvas官网blurry,Why html2canvas produced blurry pdf file?
热门文章
- 【Web渗透】Web渗透各类型问题总结
- Python写阴阳师脚本带GUI又是一个学习小技巧
- 中标丨英伟达Jetson 边缘计算模块申购、中国电信5G边缘计算应急通信车购置工程等...
- 山西大学数学与计算机科学学院,山西大学
- Mac中直接打开隐藏的文件或文件夹
- 海狮号水下机器人_【揭晓】水下机器人哪家强?冠军出炉了!
- 无线智能插座实物连线说明
- Math Type使用方法,比起word自带公式编辑器的优点总结
- Android studio实现计算BMI指数(两个页面之间的跳转)
- linux如何查看zst文件,Linux下如何查看共享文件夹