Compare commits

..

No commits in common. "b42c9200ae6f50e3dad3a85f8ec0ef054bec06ae" and "b37752f4c42022c2abc9f0938ad8d2d9d45e1c39" have entirely different histories.

4 changed files with 23 additions and 55 deletions

View File

@ -10,15 +10,15 @@ import cd.casic.ci.process.process.service.target.TargetVersionService;
import cd.casic.ci.process.util.SftpUploadUtil;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import cn.hutool.core.io.resource.InputStreamResource;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
@ -135,10 +135,10 @@ public class TargetController {
@PostMapping(path="/fileDownload")
public CommonResult<String> fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse httpResponse) throws SftpUploadUtil.SftpUploadException, IOException {
public CommonResult<InputStreamResource> fileDownload(@RequestBody @Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException {
String inputStream = targetManagerService.fileDownload(req);
InputStream inputStream = targetManagerService.fileDownload(req);
return CommonResult.success(inputStream);
return CommonResult.success(new InputStreamResource(inputStream));
}
}

View File

@ -10,7 +10,7 @@ import cd.casic.framework.commons.pojo.PageResult;
import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
@ -39,5 +39,5 @@ public interface TargetManagerService extends IService<TargetManager> {
void updateVersion(@Valid TargetVersionUpdateReq req);
String fileDownload(@Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException;
InputStream fileDownload(@Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException;
}

View File

@ -28,11 +28,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
@ -257,7 +254,7 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
}
@Override
public String fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException {
public InputStream fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException {
InputStream inputStream = SftpUploadUtil.downloadFileSftp(
req.getRemoteHost(),
req.getRemotePort(),
@ -266,22 +263,6 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
req.getSshKeyPath(),
req.getRemoteFilePath()
);
byte[] byteArray = toByteArray(inputStream);
String base64Content = Base64.getEncoder().encodeToString(byteArray);
return base64Content;
}
public static byte[] toByteArray(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[8192];
int nRead;
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
return inputStream;
}
}

View File

@ -5,8 +5,6 @@ import com.jcraft.jsch.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;
import java.util.Vector;
public class SftpUploadUtil {
@ -335,7 +333,7 @@ public class SftpUploadUtil {
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
InputStream inputStream = null;
InputStream fis = null;
try {
JSch jsch = new JSch();
@ -389,34 +387,23 @@ public class SftpUploadUtil {
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf('/') + 1);
// 切换目录并列出内容用于调试
try {
channelSftp.cd(remoteDir);
} catch (SftpException e) {
throw new SftpUploadException("切换远程目录失败: " + remoteDir, e);
}
// 列出目录内容用于调试
Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(".");
List<String> fileNames = entries.stream()
.map(entry -> entry.getFilename())
.filter(name -> !name.equals(".") && !name.equals(".."))
.toList();
System.out.println("远程目录中的文件列表: " + fileNames);
// 尝试获取文件流
try {
inputStream = channelSftp.get(fileName);
} catch (SftpException e) {
throw new SftpUploadException("获取远程文件流失败: " + fileName, e);
// 切换目录
channelSftp.cd(remoteDir);
// 获取文件属性判断是否为目录
SftpATTRS attrs = channelSftp.lstat(fileName);
if (attrs.isDir()) {
throw new SftpUploadException("目标路径是一个目录,无法下载: " + fileName);
}
// 获取文件流
InputStream inputStream = channelSftp.get(fileName);
if (inputStream == null) {
throw new SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
throw new SftpUploadException("无法获取远程文件输入流,请检查权限或路径: " + fileName);
}
return inputStream;
} catch (JSchException e) {
throw new SftpUploadException("SFTP 连接或认证失败: " + e.getMessage(), e);
} catch (SftpException e) {
@ -429,9 +416,9 @@ public class SftpUploadUtil {
throw new SftpUploadException("SFTP 上传过程中发生未知异常: " + e.getMessage(), e);
} finally {
// 9. 关闭资源 (确保在任何情况下都关闭)
if (inputStream != null) {
if (fis != null) {
try {
inputStream.close();
fis.close();
} catch (IOException e) {
System.err.println("关闭本地文件流失败: " + e.getMessage());
e.printStackTrace(); // 打印堆栈以便调试