0627 ljc 修改文件下载接口

This commit is contained in:
HopeLi 2025-06-27 17:05:55 +08:00
parent 7e10bcb7e9
commit 0689ca5412
4 changed files with 18 additions and 18 deletions

View File

@ -134,11 +134,9 @@ public class TargetController {
@PostMapping(path="/fileDownload")
public CommonResult<String> fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse httpResponse) throws SftpUploadUtil.SftpUploadException, IOException {
public void fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
targetManagerService.fileDownload(req,response);
String inputStream = targetManagerService.fileDownload(req);
return CommonResult.success(inputStream);
}
@PostMapping(path = "/uploadFolder")

View File

@ -8,6 +8,7 @@ import cd.casic.ci.process.process.dataObject.target.TargetManager;
import cd.casic.ci.process.util.SftpUploadUtil;
import cd.casic.framework.commons.pojo.PageResult;
import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;
@ -40,5 +41,5 @@ public interface TargetManagerService extends IService<TargetManager> {
void updateVersion(@Valid TargetVersionUpdateReq req);
String fileDownload(@Valid TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException;
void fileDownload(@Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException;
}

View File

@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -34,7 +35,6 @@ 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;
@ -268,19 +268,16 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
}
@Override
public String fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException {
InputStream inputStream = SftpUploadUtil.downloadFileSftp(
public void fileDownload(TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
SftpUploadUtil.downloadFileSftp(
req.getRemoteHost(),
req.getRemotePort(),
req.getUsername(),
req.getPassword(),
req.getSshKeyPath(),
req.getRemoteFilePath()
req.getRemoteFilePath(),
response
);
byte[] byteArray = toByteArray(inputStream);
String base64Content = Base64.getEncoder().encodeToString(byteArray);
return base64Content;
}
@ -292,7 +289,6 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
}

View File

@ -1,7 +1,10 @@
package cd.casic.ci.process.util;
import cd.casic.framework.commons.exception.ServiceException;
import com.amazonaws.util.IOUtils;
import com.jcraft.jsch.*;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@ -324,13 +327,14 @@ public class SftpUploadUtil {
* @param remoteFilePath 要下载的文件地址包含文件名
* @throws SftpUploadException 如果上传过程中发生任何错误
*/
public static InputStream downloadFileSftp(
public static void downloadFileSftp(
String remoteHost,
Integer remotePort,
String username,
String password,
String sshKeyPath,
String remoteFilePath) throws SftpUploadException {
String remoteFilePath,
HttpServletResponse response) throws SftpUploadException {
Session session = null;
Channel channel = null;
@ -415,8 +419,9 @@ public class SftpUploadUtil {
if (inputStream == null) {
throw new SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
}
return inputStream;
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream,outputStream);
outputStream.flush();
} catch (JSchException e) {
throw new SftpUploadException("SFTP 连接或认证失败: " + e.getMessage(), e);
} catch (SftpException e) {