双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

调用jsch包的java程序访问SFTP报错:Auth fail

zyqin zyqin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用的包是:jsch-0.1.50.jar
访问linux的sftp没有问题,可以正常上传和下载文件,但是访问solaris的sftp的时候报错:Auth fail。
SUSE 10 SP4,Solaris 10
代码如下:
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;

public class SFTPTest {

private String host;
private String username;
private String password;
private int port = 22;
private ChannelSftp sftp = null;
private Session sshSession = null;

public SFTPTest() {
}

public SFTPTest(String host, String username, String password, int port) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}

public SFTPTest(String host, String username, String password) {
this.host = host;
this.username = username;
this.password = password;
}

/**
* connect server via sftp
*/
public void connect() {
try {

JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 关闭资源
*/
public void disconnect() {
if (this.sftp != null) {
if (this.sftp.isConnected()) {
this.sftp.disconnect();
System.out.println("sftp is closed already");
}
}

if (this.sshSession != null) {
if (this.sshSession.isConnected()) {
this.sshSession.disconnect();
System.out.println("sshSession is closed already");
}

}

}

/**
* 批量下载文件
*
* @param remotPath
* 远程下载目录(以路径符号结束)
* @param localPath
* 本地保存目录(以路径符号结束)
* @param fileFormat
* 下载文件格式(以特定字符开头,为空不做检验)
* @param del
* 下载后是否删除sftp文件
* @return
*/
public boolean batchDownLoadFile(String remotPath, String localPath,
String fileFormat, boolean del) {
try {
connect();
Vector v = listFiles(remotPath);
if (v.size() > 0) {

Iterator it = v.iterator();
while (it.hasNext()) {
LsEntry entry = (LsEntry) it.next();
String filename = entry.getFilename();
SftpATTRS attrs = entry.getAttrs();
if (!attrs.isDir()) {
if (fileFormat != null && !"".equals(fileFormat.trim())) {
if (filename.startsWith(fileFormat)) {
if (this.downloadFile(remotPath, filename,
localPath, filename)
&& del) {
deleteSFTP(remotPath, filename);
}
}
} else {
if (this.downloadFile(remotPath, filename,
localPath, filename)
&& del) {
deleteSFTP(remotPath, filename);
}
}
}
}
}
} catch (SftpException e) {
e.printStackTrace();
} finally {
this.disconnect();
}
return false;
}

/**
* 下载单个文件
*
* @param remotPath
* 远程下载目录(以路径符号结束)
* @param remoteFileName
* 下载文件名
* @param localPath
* 本地保存目录(以路径符号结束)
* @param localFileName
* 保存文件名
* @return
*/
public boolean downloadFile(String remotePath, String remoteFileName,
String localPath, String localFileName) {
try {
sftp.cd(remotePath);
File file = new File(localPath + localFileName);
mkdirs(localPath + localFileName);
sftp.get(remoteFileName, new FileOutputStream(file));
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}

return false;
}

/**
* 上传单个文件
*
* @param remotePath
* 远程保存目录
* @param remoteFileName
* 保存文件名
* @param localPath
* 本地上传目录(以路径符号结束)
* @param localFileName
* 上传的文件名
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName,
String localPath, String localFileName) {
FileInputStream in = null;
try {
createDir(remotePath);
File file = new File(localPath + localFileName);
in = new FileInputStream(file);
sftp.put(in, remoteFileName);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

/**
* 批量上传文件
*
* @param remotePath
* 远程保存目录
* @param localPath
* 本地上传目录(以路径符号结束)
* @param del
* 上传后是否删除本地文件
* @return
*/
public boolean bacthUploadFile(String remotePath, String localPath,
boolean del) {
try {
connect();
File file = new File(localPath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()
&& files[i].getName().indexOf("bak") == -1) {
if (this.uploadFile(remotePath, files[i].getName(),
localPath, files[i].getName())
&& del) {
deleteFile(localPath + files[i].getName());

}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.disconnect();
}
return false;

}

/**
* 删除本地文件
*
* @param filePath
* @return
*/
public boolean deleteFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return false;
}

if (!file.isFile()) {
return false;
}

return file.delete();
}

/**
* 创建目录
*
* @param createpath
* @return
*/
public boolean createDir(String createpath) {
try {
if (isDirExist(createpath)) {
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString())) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}

}
this.sftp.cd(createpath);
return true;
} catch (SftpException e) {
e.printStackTrace();
}
return false;
}

/**
* 判断目录是否存在
*
* @param directory
* @return
*/
public boolean isDirExist(String directory) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}

/**
* 删除stfp文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
*/
public void deleteSFTP(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 如果目录不存在就创建目录
*
* @param path
*/
public void mkdirs(String path) {
File f = new File(path);

String fs = f.getParent();

f = new File(fs);

if (!f.exists()) {
f.mkdirs();
}
}

/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public ChannelSftp getSftp() {
return sftp;
}

public void setSftp(ChannelSftp sftp) {
this.sftp = sftp;
}

public static void main(String[] args) {
SFTPTest ftp = new SFTPTest("127.0.0.1", "admin", "admin");
String localPath = "D:\\sftp\\";
String remotePath = "/home/itvsoap/file_interface/bill/lzj/test/";

ftp.connect();

ftp.uploadFile(remotePath, "test.txt", localPath, "test.txt");
// ftp.bacthUploadFile(remotePath,localPath,true);
// ftp.downloadFile(remotePath, "test.txt", localPath, "test.txt");
// ftp.batchDownLoadFile(remotePath, localPath, null, true);

ftp.disconnect();
System.exit(0);
}

}
...全文

相关推荐

jsch-0.1.53.zip

jsch jar包,实现sftp连接功能,解决Algorithm negotiation fail等问题,非常的好用

qq_15539777

ExampleConnectionSSH:使用 Java 和 jsch lib 的 SSH 连接示例

示例连接SSH Java 和 JSch 的 SSH 连接示例(Java 安全通道)

孙洋 Sonya

com.jcraft.jsch

Java SSH连接远程服务器的Jar包

PoetryAndTheDistance

SFTP连接问题解决[可运行源码]

本文详细介绍了在不同操作系统环境下(包括麒麟V10、开放麒麟和欧拉环境)遇到的SFTP连接问题及其解决方案。在麒麟V10环境中,通过替换sshd_config文件解决了“algorithm negotiation fail”错误;开放麒麟环境中,手动安装ssh服务和SELinux策略包,并修正目录权限问题;欧拉环境中,通过切换至社区维护的JSch库版本解决了算法协商失败的问题。文章提供了具体的操作步骤和命令,帮助读者快速定位和解决类似问题。

Wind6

无刷直流电机BLDCM控制仿真(Simulink仿真实现)

内容概要:本文围绕无刷直流电机(BLDCM)的Simulink仿真控制展开,重点研究双PWM整流器在电机控制系统中的应用。通过构建完整的Simulink仿真模型,系统实现了对BLDCM的建模、驱动控制策略设计、动态响应分析及仿真结果验证,涵盖了电流环控制、PWM调制、转速调节等关键技术环节,旨在为电机控制系统的性能优化与工程实现提供理论支持与仿真依据。; 适合人群:具备电力电子、自动控制理论基础的研究生、从事电机控制与新能源系统研究的科研人员,以及相关领域的工程技术人员。; 使用场景及目标:①支持高校课程设计与毕业设计中电机控制系统的仿真验证;②服务于科研项目中BLDCM控制策略的开发与优化;③为工业界电机驱动系统的设计提供技术原型与方案参考。; 阅读建议:建议读者结合MATLAB/Simulink环境动手搭建模型,按照文中控制逻辑逐步实现仿真,重点关注PWM调制方式、电流环动态响应及转速控制效果,配合代码调试与波形分析,深入理解控制机理与系统特性。

程序猿鑫
评论 4
收藏 已收藏
分享
返回顶部
zyqin
Java 51,407 成员 86,133 内容