Compare commits
2 Commits
9e84324b89
...
e36d596bc7
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e36d596bc7 | ||
![]() |
741f84800a |
@ -36,7 +36,7 @@ public class SftpFileController {
|
||||
@RequestParam MultipartFile file) {
|
||||
|
||||
String localFilePath = saveTempFile(file);
|
||||
sftpFileService.uploadFile(
|
||||
String uploadFilePath = sftpFileService.uploadFile(
|
||||
fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
@ -44,10 +44,10 @@ public class SftpFileController {
|
||||
fileUploadProperties.getSshKeyPath(),
|
||||
localFilePath, remoteDir, remoteFileName);
|
||||
|
||||
return CommonResult.success("文件上传成功");
|
||||
return CommonResult.success(uploadFilePath);
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
@PostMapping("/download")
|
||||
public void downloadFile(
|
||||
@RequestParam String remoteFilePath,
|
||||
HttpServletResponse response) {
|
||||
|
@ -72,7 +72,7 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
channelSftp = (ChannelSftp) channel;
|
||||
}
|
||||
|
||||
public void uploadFile(String localFilePath, String remoteDir, String remoteFileName) throws SftpException, SftpUploadUtil.SftpUploadException, FileNotFoundException {
|
||||
public String uploadFile(String localFilePath, String remoteDir, String remoteFileName) throws SftpException, SftpUploadUtil.SftpUploadException, FileNotFoundException {
|
||||
FileInputStream fis = null;
|
||||
|
||||
// 检查并切换到远程目录
|
||||
@ -109,6 +109,8 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
// 8. 上传文件
|
||||
channelSftp.put(fis, finalRemoteFileName);
|
||||
System.out.println("文件上传成功!");
|
||||
|
||||
return remoteDir + "/" + remoteFileName;
|
||||
}
|
||||
|
||||
public void downloadFile(String remoteFilePath, OutputStream outputStream) throws SftpException, SftpUploadUtil.SftpUploadException, IOException {
|
||||
|
@ -14,7 +14,7 @@ import java.util.List;
|
||||
*/
|
||||
public interface SftpFileService{
|
||||
|
||||
void uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName);
|
||||
String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName);
|
||||
|
||||
void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response);
|
||||
|
||||
|
@ -27,9 +27,9 @@ public class SftpFileServiceImpl implements SftpFileService {
|
||||
|
||||
|
||||
@Override
|
||||
public void uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName) {
|
||||
public String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName) {
|
||||
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||
client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
||||
return client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user