import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ftp")
public class FtpFileTransferController {
@PostMapping("/login")
public ResponseEntity<FTPClient> loginToFtp(String server, int port, String user, String pass) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
return new ResponseEntity<>(ftpClient, HttpStatus.OK);
} catch (IOException e) {
System.err.println("连接或登录 FTP 服务器时出现错误: " + e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/download")
public ResponseEntity<String> downloadFile(FTPClient ftpClient, String remotePath, String localPath) {
try {
FTPFile[] files = ftpClient.listFiles(remotePath);
if (files.length > 0) {
FTPFile file = files[0];
String originalFileName = file.getName(); // 获取原文件名
// 检查本地是否存在同名文件,如果存在则添加数字后缀避免冲突
File localFile = getUniqueLocalFile(originalFileName, localPath);
OutputStream outputStream = new FileOutputStream(localFile);
try {
ftpClient.retrieveFile(remotePath, outputStream);
return new ResponseEntity<>("下载成功", HttpStatus.OK);
} catch (IOException e) {
System.err.println("下载文件时出现错误: " + e.getMessage());
return new ResponseEntity<>("下载失败", HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
try {
outputStream.close();
} catch (IOException e) {
System.err.println("关闭输出流时出现错误: " + e.getMessage());
}
}
}
return new ResponseEntity<>("远程路径下无文件可下载", HttpStatus.NOT_FOUND);
} catch (IOException e) {
System.err.println("下载文件操作时出现错误: " + e.getMessage());
return new ResponseEntity<>("下载失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(FTPClient ftpClient, String localPath, String remotePath) {
try {
File localFile = new File(localPath);
String originalFileName = localFile.getName(); // 获取原文件名
// 检查服务器端是否存在同名文件,如果存在则添加数字后缀避免冲突
String uniqueFileName = getUniqueRemoteFileName(originalFileName, remotePath, ftpClient);
FileInputStream inputStream = new FileInputStream(localFile);
try {
ftpClient.storeFile(remotePath + File.separator + uniqueFileName, inputStream);
return new ResponseEntity<>("上传成功", HttpStatus.OK);
} catch (IOException e) {
System.err.println("上传文件时出现错误: " + e.getMessage());
return new ResponseEntity<>("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
try {
inputStream.close();
} catch (IOException e) {
System.err.println("关闭输入流时出现错误: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("上传文件操作时出现错误: " + e.getMessage());
return new ResponseEntity<>("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/createFolder")
public ResponseEntity<String> createFolder(FTPClient ftpClient, String folderPath) {
try {
boolean created = ftpClient.makeDirectory(folderPath);
if (created) {
return new ResponseEntity<>("文件夹创建成功", HttpStatus.OK);
} else {
return new ResponseEntity<>("创建文件夹失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (IOException e) {
System.err.println("创建文件夹时出现错误: " + e.getMessage());
return new ResponseEntity<>("创建文件夹失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
// 检查服务器端指定路径是否存在文件
public static boolean checkFileExists(FTPClient ftpClient, String filePath) throws IOException {
try {
FTPFile[] files = ftpClient.listFiles(filePath);
return files.length > 0;
} catch (IOException e) {
System.err.println("检查服务器端文件存在性时出现错误: " + e.getMessage());
throw e;
}
}
// 给文件名添加数字后缀以避免冲突(服务器端)
public static String getUniqueRemoteFileName(String originalFileName, String remotePath, FTPClient ftpClient) {
int count = 1;
String newFileName = originalFileName;
while (checkFileExists(ftpClient, remotePath + File.separator + newFileName)) {
newFileName = getFileNameWithSuffix(originalFileName, count++);
}
return newFileName;
}
// 给文件名添加数字后缀以避免冲突(本地端)
public static File getUniqueLocalFile(String originalFileName, String localPath) {
int count = 1;
String newFileName = originalFileName;
File file = new File(localPath + File.separator + newFileName);
while (file.exists()) {
newFileName = getFileNameWithSuffix(originalFileName, count++);
file = new File(localPath + File.separator + newFileName);
}
return file;
}
// 获取带有数字后缀的文件名
public static String getFileNameWithSuffix(String originalFileName, int count) {
String fileNameWithoutExtension = originalFileName.substring(0, originalFileName.lastIndexOf('.'));
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf('.'));
return fileNameWithoutExtension + "_" + count + fileExtension;
}
}