当前位置:网站首页>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();
}
}
}边栏推荐
- The first demand in my life - batch uploading of Excel data to the database
- Personalized federated learning using hypernetworks paper reading notes + code interpretation
- R语言使用neuralnet包构建神经网络回归模型(前馈神经网络回归模型),计算模型在测试集上的MSE值(均方误差)
- Arrangement of statistical learning knowledge points -- maximum likelihood estimation (MLE) and maximum a posteriori probability (map)
- 解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
- Adaptive personalized federated learning paper interpretation + code analysis
- Voice assistant - Measurement Indicators
- Voice assistant - potential skills and uncalled call technique mining
- Continuous local training for better initialization of Federated models
- Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
猜你喜欢

Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method

Modelarts training task 1

Question bank and answers of special operation certificate examination for safety management personnel of hazardous chemical business units in 2022

Topic 1 Single_Cell_analysis(4)

2022r2 mobile pressure vessel filling test question simulation test platform operation
![[yolo-v5 learning notes]](/img/f8/713210cafd7b750df540acbe03fd29.jpg)
[yolo-v5 learning notes]

Voice assistant - Qu - ner and intention slot model

2022 G3 boiler water treatment recurrent training question bank and answers

謀新局、促發展,桂林綠色數字經濟的頭雁效應

In depth learning - overview of image classification related models
随机推荐
2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
L'effet de l'oie sauvage sur l'économie numérique verte de Guilin
tmux 和 vim 的快捷键修改
Leetcode notes: Weekly contest 296
How to stop MySQL service under Linux
Improved schemes for episodic memory based lifelong learning
解决逆向工程Mapper重复问题
BI技巧丨当月期初
Summary of machine learning + pattern recognition learning (VI) -- feature selection and feature extraction
LeetCode34. 在排序数组中查找元素的第一个和最后一个位置
Multithread decompression of tar
Bi skills - beginning of the month
Chapter 4 - key management and distribution
MySQL索引(一篇文章轻松搞定)
Continuous local training for better initialization of Federated models
Personalized federated learning with exact stochastic gradient descent
Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
R language uses rstudio to save visualization results as PDF files (export--save as PDF)
vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)
Gd32f4 (5): gd32f450 clock is configured as 200m process analysis