0725 ljc 修改sftpClientUtils位置

This commit is contained in:
HopeLi 2025-07-25 15:50:46 +08:00
parent 2ce5f03f88
commit 56ef0b77dc
3 changed files with 36 additions and 17 deletions

View File

@ -27,6 +27,16 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </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> </dependencies>
</project> </project>

View File

@ -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 cd.casic.framework.commons.exception.ServiceException;
import com.amazonaws.util.IOUtils; import com.amazonaws.util.IOUtils;
import com.jcraft.jsch.*; import com.jcraft.jsch.*;
@ -23,7 +22,7 @@ public class SftpClientUtils implements AutoCloseable {
private ChannelSftp channelSftp; private ChannelSftp channelSftp;
public SftpClientUtils(String remoteHost, Integer remotePort, String username, 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(); JSch jsch = new JSch();
// 1. 添加身份认证信息 (密码或密钥) // 1. 添加身份认证信息 (密码或密钥)
@ -31,13 +30,13 @@ public class SftpClientUtils implements AutoCloseable {
// 使用 SSH Key 认证 // 使用 SSH Key 认证
File sshKeyFile = new File(sshKeyPath); File sshKeyFile = new File(sshKeyPath);
if (!sshKeyFile.exists() || !sshKeyFile.isFile()) { if (!sshKeyFile.exists() || !sshKeyFile.isFile()) {
throw new SftpUploadUtil.SftpUploadException("SSH Key 文件不存在或不是一个有效文件: " + sshKeyPath); throw new SftpClientUtils.SftpUploadException("SSH Key 文件不存在或不是一个有效文件: " + sshKeyPath);
} }
jsch.addIdentity(sshKeyPath); jsch.addIdentity(sshKeyPath);
System.out.println("使用 SSH Key 认证: " + sshKeyPath); System.out.println("使用 SSH Key 认证: " + sshKeyPath);
} else if (password == null || password.trim().isEmpty()) { } else if (password == null || password.trim().isEmpty()) {
// 如果没有提供密码或密钥路径则认证信息不全 // 如果没有提供密码或密钥路径则认证信息不全
throw new SftpUploadUtil.SftpUploadException("必须提供密码或 SSH Key 路径进行 SFTP 认证."); throw new SftpClientUtils.SftpUploadException("必须提供密码或 SSH Key 路径进行 SFTP 认证.");
} }
// 如果提供了密码将在 getSession 后设置因为 getSession 需要用户名主机和端口先建立连接意图 // 如果提供了密码将在 getSession 后设置因为 getSession 需要用户名主机和端口先建立连接意图
@ -72,7 +71,7 @@ public class SftpClientUtils implements AutoCloseable {
channelSftp = (ChannelSftp) channel; 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; FileInputStream fis = null;
// 检查并切换到远程目录 // 检查并切换到远程目录
@ -89,14 +88,14 @@ public class SftpClientUtils implements AutoCloseable {
System.out.println("远程目录创建成功并已切换: " + remoteDir); System.out.println("远程目录创建成功并已切换: " + remoteDir);
} catch (SftpException e2) { } catch (SftpException e2) {
// 创建目录失败 // 创建目录失败
throw new SftpUploadUtil.SftpUploadException("远程目录创建失败: " + remoteDir, e2); throw new SftpClientUtils.SftpUploadException("远程目录创建失败: " + remoteDir, e2);
} }
} }
// 6. 获取本地文件流 // 6. 获取本地文件流
File localFile = new File(localFilePath); File localFile = new File(localFilePath);
if (!localFile.exists() || !localFile.isFile()) { if (!localFile.exists() || !localFile.isFile()) {
throw new SftpUploadUtil.SftpUploadException("本地文件不存在或不是一个有效文件: " + localFilePath); throw new SftpClientUtils.SftpUploadException("本地文件不存在或不是一个有效文件: " + localFilePath);
} }
fis = new FileInputStream(localFile); fis = new FileInputStream(localFile);
System.out.println("本地文件流获取成功: " + localFilePath); System.out.println("本地文件流获取成功: " + localFilePath);
@ -113,7 +112,7 @@ public class SftpClientUtils implements AutoCloseable {
return remoteDir + "/" + remoteFileName; 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; InputStream inputStream = null;
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/')); String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
@ -123,7 +122,7 @@ public class SftpClientUtils implements AutoCloseable {
try { try {
channelSftp.cd(remoteDir); channelSftp.cd(remoteDir);
} catch (SftpException e) { } 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); inputStream = channelSftp.get(fileName);
if (inputStream == null) { if (inputStream == null) {
throw new SftpUploadUtil.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName); throw new SftpClientUtils.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
} }
IOUtils.copy(inputStream,outputStream); IOUtils.copy(inputStream,outputStream);
outputStream.flush(); outputStream.flush();
} catch (SftpException e) { } catch (SftpException e) {
throw new SftpUploadUtil.SftpUploadException("文件下载失败: " + remoteFilePath, e); throw new SftpClientUtils.SftpUploadException("文件下载失败: " + remoteFilePath, e);
} finally { } finally {
if (inputStream != null) { if (inputStream != null) {
try { 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; InputStream inputStream = null;
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/')); String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
@ -168,7 +167,7 @@ public class SftpClientUtils implements AutoCloseable {
try { try {
channelSftp.cd(remoteDir); channelSftp.cd(remoteDir);
} catch (SftpException e) { } 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); inputStream = channelSftp.get(fileName);
if (inputStream == null) { if (inputStream == null) {
throw new SftpUploadUtil.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName); throw new SftpClientUtils.SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
} }
return inputStream; return inputStream;
} catch (SftpException e) { } 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 @Override
public void close() { public void close() {
if (channelSftp != null) { if (channelSftp != null) {

View File

@ -1,7 +1,7 @@
package cd.casic.ci.process.process.service.sftpFile.impl; 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.process.service.sftpFile.SftpFileService;
import cd.casic.ci.process.properties.TargetFileUploadProperties; import cd.casic.ci.process.properties.TargetFileUploadProperties;
import com.amazonaws.util.IOUtils; import com.amazonaws.util.IOUtils;