0605 ljc 报告模块修改

This commit is contained in:
HopeLi 2025-06-06 18:11:00 +08:00
parent 7079de93ff
commit 492d07c683
4 changed files with 55 additions and 23 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.InputStream;
import java.io.IOException;
import java.util.List;
/**
@ -135,10 +135,10 @@ public class TargetController {
@PostMapping(path="/fileDownload")
public CommonResult<InputStreamResource> fileDownload(@RequestBody @Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException {
public CommonResult<String> fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse httpResponse) throws SftpUploadUtil.SftpUploadException, IOException {
InputStream inputStream = targetManagerService.fileDownload(req);
String inputStream = targetManagerService.fileDownload(req);
return CommonResult.success(new InputStreamResource(inputStream));
return CommonResult.success(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.InputStream;
import java.io.IOException;
import java.util.List;
/**
@ -39,5 +39,5 @@ public interface TargetManagerService extends IService<TargetManager> {
void updateVersion(@Valid TargetVersionUpdateReq req);
InputStream fileDownload(@Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException;
String fileDownload(@Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException;
}

View File

@ -28,8 +28,11 @@ 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;
@ -254,7 +257,7 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
}
@Override
public InputStream fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException {
public String fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException {
InputStream inputStream = SftpUploadUtil.downloadFileSftp(
req.getRemoteHost(),
req.getRemotePort(),
@ -263,6 +266,22 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
req.getSshKeyPath(),
req.getRemoteFilePath()
);
return inputStream;
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();
}
}

View File

@ -5,6 +5,8 @@ import com.jcraft.jsch.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;
import java.util.Vector;
public class SftpUploadUtil {
@ -333,7 +335,7 @@ public class SftpUploadUtil {
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
InputStream fis = null;
InputStream inputStream = null;
try {
JSch jsch = new JSch();
@ -387,23 +389,34 @@ public class SftpUploadUtil {
String remoteDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/'));
String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf('/') + 1);
// 切换目录
channelSftp.cd(remoteDir);
// 获取文件属性判断是否为目录
SftpATTRS attrs = channelSftp.lstat(fileName);
if (attrs.isDir()) {
throw new SftpUploadException("目标路径是一个目录,无法下载: " + fileName);
// 切换目录并列出内容用于调试
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);
}
// 获取文件流
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) {
@ -416,9 +429,9 @@ public class SftpUploadUtil {
throw new SftpUploadException("SFTP 上传过程中发生未知异常: " + e.getMessage(), e);
} finally {
// 9. 关闭资源 (确保在任何情况下都关闭)
if (fis != null) {
if (inputStream != null) {
try {
fis.close();
inputStream.close();
} catch (IOException e) {
System.err.println("关闭本地文件流失败: " + e.getMessage());
e.printStackTrace(); // 打印堆栈以便调试