当前位置:网站首页>解决上传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();
}
}
}边栏推荐
- 2022起重机械指挥考试题模拟考试平台操作
- Summary of machine learning + pattern recognition learning (I) -- k-nearest neighbor method
- Hongmeng OS first training
- Summary of machine learning + pattern recognition learning (VI) -- feature selection and feature extraction
- Use case design of software testing interview questions
- 数值计算方法 Chapter5. 解线性方程组的直接法
- Non IID data and continuous learning processes in federated learning: a long road ahead
- There is no solid line connection between many devices in Proteus circuit simulation design diagram. How are they realized?
- Learning to continuously learn paper notes + code interpretation
- Detailed explanation of memory addressing in 8086 real address mode
猜你喜欢

Detailed explanation of TF2 command line debugging tool in ROS (parsing + code example + execution logic)

Voice assistant - Multi round conversation (process implementation)

BI技巧丨当月期初

Personalized federated learning using hypernetworks paper reading notes + code interpretation

晶闸管,它是很重要的,交流控制器件

Nine project management issues that PM should understand

Utilize user behavior data

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

AI狂想|来这场大会,一起盘盘 AI 的新工具!

Summary of semantic segmentation learning (I) -- basic concepts
随机推荐
Detailed explanation of 8086/8088 system bus (sequence analysis + bus related knowledge)
2022年危险化学品经营单位安全管理人员特种作业证考试题库及答案
modelarts二
5 lines of code identify various verification codes
AcWing——4268. Sexy element
Generalized semantic recognition based on semantic similarity
Construction of running water lamp experiment with simulation software proteus
2022起重机械指挥考试题模拟考试平台操作
Voice assistant - Introduction and interaction process
Detailed explanation of 14 registers in 8086CPU
鸿蒙os-第一次培训
Test left shift real introduction
Gradient epic memory for continuous learning
LeetCode笔记:Weekly Contest 296
晶闸管,它是很重要的,交流控制器件
AI fanaticism | come to this conference and work together on the new tools of AI!
Exposure compensation, white increase and black decrease theory
Missing getting in online continuous learning with neuron calibration thesis analysis + code reading
LED lighting experiment with simulation software proteus
Support vector machine (SVM)