当前位置:网站首页>Solve the problem of uploading sftporg apache. commons. net. MalformedServerReplyException: Could not parse respon
Solve the problem of uploading sftporg apache. commons. net. MalformedServerReplyException: Could not parse respon
2022-06-12 07:43:00 【Conquer!】
Solve last time SFTP Report errors :org.apache.commons.net.MalformedServerReplyException: Could not parse respon; Server Reply: SSH-2.0-OpenSSH_5.3
reason : When using org.apache.commons.net.ftp.FTPClient By agreement SSH2 Conduct SFTP Connection error , The reason is that it does not support this kind of connection
terms of settlement
Use 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 {
// user name
private String username;
// password
private String password;
//ip
private String host;
// The port is generally 22
private int port;
// Private key
private String privateKey;
ChannelSftp sftp = null;
// Pass parameters through the construction method
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;
}
// Sign in , Check the links
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(" Login successful ");
} catch (JSchException e) {
e.printStackTrace();
}
}
// Upload files
/**
* @param basePath The target path
* @param direcotry Target subpath
* @param sftpFileName File name
*/
public void upload(String basePath, String direcotry, String sftpFileName, InputStream inputStream) throws SftpException{
try {
// Go to the target directory
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(" Upload successful ");
}
// Download the file
/**
* @param directory Download file path
* @param downloadFile Download file name
* @param saveFileDirectory Saved file path
*/
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(" Download successful ");
}
// Log out
public static void main(String[] args) throws FileNotFoundException,SftpException {
FileUtil fiel = new FileUtil(" user name "," password ","host",22);
fiel.login();
// Test upload function
File file = new File( Picture address );
InputStream is = new FileInputStream(file);
fiel.upload(" Upload target directory ",""," file name .xlxs",is);
// Test the download function
try {
fiel.download(" Download target directory "," Download filename "," Download to the path ( Local server path )");
} catch (SftpException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}边栏推荐
- 解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
- [tutorial] deployment process of yolov5 based on tensorflow Lite
- Velocity autocorrelation function lammps v.s MATALB
- R语言将dataframe数据中指定数据列的数据从小数转化为百分比表示、数据转换为百分数
- The latest hbuilderx editing uni app project runs in the night God simulator
- Leetcode notes: Weekly contest 296
- tmux 和 vim 的快捷键修改
- Golang quickly generates model and queryset of database tables
- Voice assistant -- Qu -- query error correction and rewriting
- Voice assistant - future trends
猜你喜欢

Summary of machine learning + pattern recognition learning (IV) -- decision tree

vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)

Exposure compensation, white increase and black decrease theory

Personalized federated learning with Moreau envelopes

Bi skills - beginning of the month

Topic 1 Single_Cell_analysis(3)

Summary of machine learning + pattern recognition learning (V) -- Integrated Learning

Missing getting in online continuous learning with neuron calibration thesis analysis + code reading
![[tutorial] deployment process of yolov5 based on tensorflow Lite](/img/d0/c38f27ad76b62b27cdeb68728e9c8c.jpg)
[tutorial] deployment process of yolov5 based on tensorflow Lite

Golang 快速生成数据库表的 model 和 queryset
随机推荐
Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method
Acwing - 4269 school anniversary
最新hbuilderX编辑uni-app项目运行于夜神模拟器
R语言caTools包进行数据划分、scale函数进行数据缩放、class包的knn函数构建K近邻分类器、比较不同K值超参数下模型准确率(accuracy)
20220526 损失函数
Scoring prediction problem
R语言rnorm函数生成正太分布数据、使用epiDisplay包的summ函数计算向量数据的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
GD32F4(5):GD32F450时钟配置为200M过程分析
初步认知Next.js中ISR/RSC/Edge Runtime/Streaming等新概念
R语言使用RStudio将可视化结果保存为pdf文件(export--Save as PDF)
LeetCode笔记:Weekly Contest 296
Voice assistant - Qu - single entity recall
20220524 深度学习技术点
Vs2019 MFC IP address Control Control inherit cipaddressctrl class redessine
Voice assistant - Introduction and interaction process
C language sizeof strlen
2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
二、八、十、十六进制相互转换
Model deployment learning notes (I)
Voice assistant - Measurement Indicators