当前位置:网站首页>解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
2022-06-12 07:35:00 【Conquer!】
解决上次SFTP报错:org.apache.commons.net.MalformedServerReplyException: Could not parse respon; Server Reply: SSH-2.0-OpenSSH_5.3
原因:当使用org.apache.commons.net.ftp.FTPClient通过协议SSH2进行SFTP连接时报如上错误,原因是它不支持这种方式的连接
解决办法
使用 com.jcraft
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>import com.jcraft.jsch.*;
import java.io.*;
import java.util.Properties;
public class FileUtil {
//用户名
private String username;
//密码
private String password;
//ip
private String host;
//端口一般为22
private int port;
//私钥
private String privateKey;
ChannelSftp sftp = null;
//通过构造方法传参
public FileUtil(String username, String password, String host, int port){
this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
public FileUtil(String username, String host, int port, String privateKey){
this.username = username;
this.host = host;
this.port = port;
this.privateKey = privateKey;
}
//登录,检查链接情况
public void login(){
try {
JSch jSch = new JSch();
if(privateKey != null){
jSch.addIdentity(privateKey);
}
Session session = jSch.getSession(username,host,port);
if(password != null){
session.setPassword(password);
}
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("登录成功");
} catch (JSchException e) {
e.printStackTrace();
}
}
//上传文件
/**
* @param basePath 目标路径
* @param direcotry 目标子路径
* @param sftpFileName 文件名称
*/
public void upload(String basePath, String direcotry, String sftpFileName, InputStream inputStream) throws SftpException{
try {
//进入到目标目录
sftp.cd(basePath);
sftp.cd(direcotry);
} catch (SftpException e) {
String[] dirs = direcotry.split("/");
String temPath = basePath;
for (String dir: dirs
) {
if( null == dir || "".equals(dir)) continue;
temPath +="/" + dir;
try {
sftp.cd(temPath);
} catch (SftpException ex) {
sftp.mkdir(temPath);
sftp.cd(temPath);
}
}
}
sftp.put(inputStream,sftpFileName);
System.out.println("上传成功");
}
//下载文件
/**
* @param directory 下载的文件路径
* @param downloadFile 下载的文件名
* @param saveFileDirectory 保存的文件路径
*/
public void download(String directory, String downloadFile, String saveFileDirectory) throws SftpException, FileNotFoundException{
if(directory != null && !"".equals(directory)){
sftp.cd(directory);
}
String saveFile = saveFileDirectory + "//" + downloadFile;
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
System.out.println("下载成功");
}
//登出
public static void main(String[] args) throws FileNotFoundException,SftpException {
FileUtil fiel = new FileUtil("用户名","密码","host",22);
fiel.login();
//测试上传功能
File file = new File(图片地址);
InputStream is = new FileInputStream(file);
fiel.upload("上传目标目录","","文件名.xlxs",is);
//测试下载功能
try {
fiel.download("下载目标目录","下载文件名","下载到的路径(本地服务器路径)");
} catch (SftpException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}边栏推荐
- Hongmeng OS first training
- R语言dplyr包mutate_at函数和one_of函数将dataframe数据中指定数据列(通过向量指定)的数据类型转化为因子类型
- Paddepaddl 28 supports the implementation of GHM loss, a gradient balancing mechanism for arbitrary dimensional data (supports ignore\u index, class\u weight, back propagation training, and multi clas
- Chapter 2 - cyber threats and attacks
- FCPX插件:简约线条呼出文字标题介绍动画Call Outs With Photo Placeholders for FCPX
- 最新hbuilderX编辑uni-app项目运行于夜神模拟器
- Summary of machine learning + pattern recognition learning (IV) -- decision tree
- Chapter 6 - identity authentication, Chapter 7 - access control
- 2022 electrician (elementary) examination question bank and simulation examination
- QT realization tray
猜你喜欢

谋新局、促发展,桂林绿色数字经济的头雁效应

What is a good recommendation system?

Arrangement of statistical learning knowledge points -- maximum likelihood estimation (MLE) and maximum a posteriori probability (map)

Detailed explanation of coordinate tracking of TF2 operation in ROS (example + code)

MySQL index (easy to handle in one article)

Detailed explanation of 8086/8088 system bus (sequence analysis + bus related knowledge)

Voice assistant - Measurement Indicators

Explain ADC in stm32

Use of gt911 capacitive touch screen

Generalized semantic recognition based on semantic similarity
随机推荐
Voice assistant - those classification models used in the assistant
Cold start problem of recommended system
Chapter 3 - Fundamentals of cryptography
Voice assistant - Qu - ner and intention slot model
R语言caTools包进行数据划分、scale函数进行数据缩放、class包的knn函数构建K近邻分类器、比较不同K值超参数下模型准确率(accuracy)
面试计算机网络-传输层
Chapter 8 - firewall, Chapter 9 - Intrusion Detection
Voice assistant - Introduction and interaction process
C language sizeof strlen
Thyristor, it is a very important AC control device
Shortcut key modification of TMUX and VIM
速度自关联函数—LAMMPS V.S MATALB
Golang 快速生成数据库表的 model 和 queryset
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图、使用dot.col参数设置不同分组数据点的颜色
R语言将dataframe数据中指定数据列的数据从小数转化为百分比表示、数据转换为百分数
vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)
连接数据库却无法修改数据
Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method
[tutorial] deployment process of yolov5 based on tensorflow Lite
Velocity autocorrelation function lammps v.s MATALB