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