0725 ljc 修改sftpClientUtils位置
This commit is contained in:
parent
2ce5f03f88
commit
56ef0b77dc
@ -27,6 +27,16 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jsch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-core</artifactId>
|
||||
<version>1.12.777</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,6 +1,5 @@
|
||||
package cd.casic.ci.process.process.service.sftpFile;
|
||||
package cd.casic.ci.commons.sftp;
|
||||
|
||||
import cd.casic.ci.process.util.SftpUploadUtil;
|
||||
import cd.casic.framework.commons.exception.ServiceException;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
import com.jcraft.jsch.*;
|
||||
@ -23,7 +22,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
private ChannelSftp channelSftp;
|
||||
|
||||
public SftpClientUtils(String remoteHost, Integer remotePort, String username,
|
||||
String password, String sshKeyPath) throws JSchException, SftpException, SftpUploadUtil.SftpUploadException {
|
||||
String password, String sshKeyPath) throws JSchException, SftpException, SftpClientUtils.SftpUploadException {
|
||||
JSch jsch = new JSch();
|
||||
|
||||
// 1. 添加身份认证信息 (密码或密钥)
|
||||
@ -31,13 +30,13 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
// 使用 SSH Key 认证
|
||||
File sshKeyFile = new File(sshKeyPath);
|
||||
if (!sshKeyFile.exists() || !sshKeyFile.isFile()) {
|
||||
throw new SftpUploadUtil.SftpUploadException("SSH Key 文件不存在或不是一个有效文件: " + sshKeyPath);
|
||||
throw new SftpClientUtils.SftpUploadException("SSH Key 文件不存在或不是一个有效文件: " + sshKeyPath);
|
||||
}
|
||||
jsch.addIdentity(sshKeyPath);
|
||||
System.out.println("使用 SSH Key 认证: " + sshKeyPath);
|
||||
} else if (password == null || password.trim().isEmpty()) {
|
||||
// 如果没有提供密码或密钥路径,则认证信息不全
|
||||
throw new SftpUploadUtil.SftpUploadException("必须提供密码或 SSH Key 路径进行 SFTP 认证.");
|
||||
throw new SftpClientUtils.SftpUploadException("必须提供密码或 SSH Key 路径进行 SFTP 认证.");
|
||||
}
|
||||
// 如果提供了密码,将在 getSession 后设置,因为 getSession 需要用户名、主机和端口先建立连接意图
|
||||
|
||||
@ -72,7 +71,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
channelSftp = (ChannelSftp) channel;
|
||||
}
|
||||
|
||||
public String uploadFile(String localFilePath, String remoteDir, String remoteFileName) throws SftpException, SftpUploadUtil.SftpUploadException, FileNotFoundException {
|
||||
public String uploadFile(String localFilePath, String remoteDir, String remoteFileName) throws SftpException, SftpClientUtils.SftpUploadException, FileNotFoundException {
|
||||
FileInputStream fis = null;
|
||||
|
||||
// 检查并切换到远程目录
|
||||
@ -89,14 +88,14 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
System.out.println("远程目录创建成功并已切换: " + remoteDir);
|
||||
} catch (SftpException e2) {
|
||||
// 创建目录失败
|
||||
throw new SftpUploadUtil.SftpUploadException("远程目录创建失败: " + remoteDir, e2);
|
||||
throw new SftpClientUtils.SftpUploadException("远程目录创建失败: " + remoteDir, e2);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 获取本地文件流
|
||||
File localFile = new File(localFilePath);
|
||||
if (!localFile.exists() || !localFile.isFile()) {
|
||||
throw new SftpUploadUtil.SftpUploadException("本地文件不存在或不是一个有效文件: " + localFilePath);
|
||||
throw new SftpClientUtils.SftpUploadException("本地文件不存在或不是一个有效文件: " + localFilePath);
|
||||
}
|
||||
fis = new FileInputStream(localFile);
|
||||
System.out.println("本地文件流获取成功: " + localFilePath);
|
||||
@ -113,7 +112,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
return remoteDir + "/" + remoteFileName;
|
||||
}
|
||||
|
||||
public void downloadFile(String remoteFilePath, OutputStream outputStream) throws SftpException, SftpUploadUtil.SftpUploadException, IOException {
|
||||
public void downloadFile(String remoteFilePath, OutputStream outputStream) throws SftpException, SftpClientUtils.SftpUploadException, IOException {
|
||||
InputStream inputStream = null;
|
||||
|
||||
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
|
||||
@ -123,7 +122,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
try {
|
||||
channelSftp.cd(remoteDir);
|
||||
} catch (SftpException e) {
|
||||
throw new SftpUploadUtil.SftpUploadException("切换远程目录失败: " + remoteDir, e);
|
||||
throw new SftpClientUtils.SftpUploadException("切换远程目录失败: " + remoteDir, e);
|
||||
}
|
||||
|
||||
// 列出目录内容用于调试
|
||||
@ -140,13 +139,13 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
inputStream = channelSftp.get(fileName);
|
||||
|
||||
if (inputStream == null) {
|
||||
throw new SftpUploadUtil.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
||||
throw new SftpClientUtils.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
||||
}
|
||||
|
||||
IOUtils.copy(inputStream,outputStream);
|
||||
outputStream.flush();
|
||||
} catch (SftpException e) {
|
||||
throw new SftpUploadUtil.SftpUploadException("文件下载失败: " + remoteFilePath, e);
|
||||
throw new SftpClientUtils.SftpUploadException("文件下载失败: " + remoteFilePath, e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
@ -158,7 +157,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream downloadFileToStream(String remoteFilePath) throws SftpException, SftpUploadUtil.SftpUploadException {
|
||||
public InputStream downloadFileToStream(String remoteFilePath) throws SftpException, SftpClientUtils.SftpUploadException {
|
||||
InputStream inputStream = null;
|
||||
|
||||
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
|
||||
@ -168,7 +167,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
try {
|
||||
channelSftp.cd(remoteDir);
|
||||
} catch (SftpException e) {
|
||||
throw new SftpUploadUtil.SftpUploadException("切换远程目录失败: " + remoteDir, e);
|
||||
throw new SftpClientUtils.SftpUploadException("切换远程目录失败: " + remoteDir, e);
|
||||
}
|
||||
|
||||
// 列出目录内容用于调试
|
||||
@ -185,12 +184,12 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
inputStream = channelSftp.get(fileName);
|
||||
|
||||
if (inputStream == null) {
|
||||
throw new SftpUploadUtil.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
||||
throw new SftpClientUtils.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
} catch (SftpException e) {
|
||||
throw new SftpUploadUtil.SftpUploadException("获取远程文件流失败: " + fileName, e);
|
||||
throw new SftpClientUtils.SftpUploadException("获取远程文件流失败: " + fileName, e);
|
||||
}
|
||||
|
||||
|
||||
@ -242,6 +241,16 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
// 自定义异常类,用于封装上传过程中的错误
|
||||
public static class SftpUploadException extends Exception {
|
||||
public SftpUploadException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public SftpUploadException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (channelSftp != null) {
|
@ -1,7 +1,7 @@
|
||||
package cd.casic.ci.process.process.service.sftpFile.impl;
|
||||
|
||||
|
||||
import cd.casic.ci.process.process.service.sftpFile.SftpClientUtils;
|
||||
import cd.casic.ci.commons.sftp.SftpClientUtils;
|
||||
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
||||
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
|
Loading…
x
Reference in New Issue
Block a user