解决上次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();
}
}
}
本文介绍了在遇到使用Apache Commons Net的FTPClient通过SSH2协议进行SFTP连接时出现的MalformedServerReplyException错误的原因及解决方案。通过引入JSch库,实现了SFTP的正确连接、文件上传和下载功能。示例代码详细展示了如何使用JSch进行登录、上传和下载操作。
2387

被折叠的 条评论
为什么被折叠?



