0627 ljc 修改文件下载接口
This commit is contained in:
parent
7e10bcb7e9
commit
0689ca5412
@ -134,11 +134,9 @@ public class TargetController {
|
|||||||
|
|
||||||
|
|
||||||
@PostMapping(path="/fileDownload")
|
@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")
|
@PostMapping(path = "/uploadFolder")
|
||||||
|
@ -8,6 +8,7 @@ import cd.casic.ci.process.process.dataObject.target.TargetManager;
|
|||||||
import cd.casic.ci.process.util.SftpUploadUtil;
|
import cd.casic.ci.process.util.SftpUploadUtil;
|
||||||
import cd.casic.framework.commons.pojo.PageResult;
|
import cd.casic.framework.commons.pojo.PageResult;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -40,5 +41,5 @@ public interface TargetManagerService extends IService<TargetManager> {
|
|||||||
|
|
||||||
void updateVersion(@Valid TargetVersionUpdateReq req);
|
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;
|
||||||
}
|
}
|
||||||
|
@ -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.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -34,7 +35,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -268,19 +268,16 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String fileDownload(TargetDownloadReq req) throws SftpUploadUtil.SftpUploadException, IOException {
|
public void fileDownload(TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
||||||
InputStream inputStream = SftpUploadUtil.downloadFileSftp(
|
SftpUploadUtil.downloadFileSftp(
|
||||||
req.getRemoteHost(),
|
req.getRemoteHost(),
|
||||||
req.getRemotePort(),
|
req.getRemotePort(),
|
||||||
req.getUsername(),
|
req.getUsername(),
|
||||||
req.getPassword(),
|
req.getPassword(),
|
||||||
req.getSshKeyPath(),
|
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) {
|
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||||
buffer.write(data, 0, nRead);
|
buffer.write(data, 0, nRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.toByteArray();
|
return buffer.toByteArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package cd.casic.ci.process.util;
|
package cd.casic.ci.process.util;
|
||||||
|
|
||||||
import cd.casic.framework.commons.exception.ServiceException;
|
import cd.casic.framework.commons.exception.ServiceException;
|
||||||
|
import com.amazonaws.util.IOUtils;
|
||||||
import com.jcraft.jsch.*;
|
import com.jcraft.jsch.*;
|
||||||
|
import jakarta.servlet.ServletOutputStream;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -324,13 +327,14 @@ public class SftpUploadUtil {
|
|||||||
* @param remoteFilePath 要下载的文件地址(包含文件名)
|
* @param remoteFilePath 要下载的文件地址(包含文件名)
|
||||||
* @throws SftpUploadException 如果上传过程中发生任何错误
|
* @throws SftpUploadException 如果上传过程中发生任何错误
|
||||||
*/
|
*/
|
||||||
public static InputStream downloadFileSftp(
|
public static void downloadFileSftp(
|
||||||
String remoteHost,
|
String remoteHost,
|
||||||
Integer remotePort,
|
Integer remotePort,
|
||||||
String username,
|
String username,
|
||||||
String password,
|
String password,
|
||||||
String sshKeyPath,
|
String sshKeyPath,
|
||||||
String remoteFilePath) throws SftpUploadException {
|
String remoteFilePath,
|
||||||
|
HttpServletResponse response) throws SftpUploadException {
|
||||||
|
|
||||||
Session session = null;
|
Session session = null;
|
||||||
Channel channel = null;
|
Channel channel = null;
|
||||||
@ -415,8 +419,9 @@ public class SftpUploadUtil {
|
|||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
throw new SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
throw new SftpUploadException("无法获取远程文件输入流,请检查文件是否存在或被其他进程占用: " + fileName);
|
||||||
}
|
}
|
||||||
|
ServletOutputStream outputStream = response.getOutputStream();
|
||||||
return inputStream;
|
IOUtils.copy(inputStream,outputStream);
|
||||||
|
outputStream.flush();
|
||||||
} catch (JSchException e) {
|
} catch (JSchException e) {
|
||||||
throw new SftpUploadException("SFTP 连接或认证失败: " + e.getMessage(), e);
|
throw new SftpUploadException("SFTP 连接或认证失败: " + e.getMessage(), e);
|
||||||
} catch (SftpException e) {
|
} catch (SftpException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user