从ftp同步文件到本地
1、可覆盖原tp 所在的原文件夹和原文件
public void copyRemoteFolderToLocal(String remoteFolder, String localFolder) throws IOException {
// 创建FTP连接
FTPClient ftpClient = new FTPClient();
ftpClient.connect("192.168.0.106", 21);
ftpClient.login("manman", "1213");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
// 判断本地文件夹是否存在
File localFolderFile = new File(localFolder);
if (!localFolderFile.exists()) {
localFolderFile.mkdirs();
}
// 判断远程文件夹是否存在
if (!ftpClient.changeWorkingDirectory(remoteFolder)) {
throw new IOException("Remote folder not exists: " + remoteFolder);
}
// 列出远程文件夹下的所有文件及文件夹
FTPFile[] ftpFiles = ftpClient.listFiles(remoteFolder);
// 递归复制远程文件夹及文件到本地
for (FTPFile ftpFile : ftpFiles) {
String fileName = ftpFile.getName();
if (fileName.equals(".") || fileName.equals("..")) {
continue;
}
String remoteFilePath = remoteFolder + "/" + fileName;
String localFilePath = localFolder + "/" + fileName;
if (ftpFile.isDirectory()) {
copyRemoteFolderToLocal(remoteFilePath, localFilePath);
} else {
OutputStream outputStream = new FileOutputStream(localFilePath);
ftpClient.retrieveFile(remoteFilePath, outputStream);
outputStream.close();
}
}
// 关闭FTP连接
ftpClient.logout();
ftpClient.disconnect();
}
2.可覆盖原tp 所在的原文件夹和原文件
public void copyDirectory(String remoteSrcPath, String localDestPath) throws IOException {
copyDirectory(remoteSrcPath, "", localDestPath);
}
private void copyDirectory(String remoteSrcPath, String localSrcPath, String localDestPath) throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect("192.168.0.106", 21);
ftp.login("manman", "1213");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remoteSrcPath);
File localDestDir = new File(localDestPath);
if (!localDestDir.exists() && !localDestDir.mkdir()) {
throw new IOException("Failed to create local directory: " + localDestPath);
}
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
String remoteFilePath = remoteSrcPath + "/" + file.getName();
String localFilePath = localDestPath + "/" + file.getName();
if (file.isFile()) {
try (InputStream outputStream = ftp.retrieveFileStream(remoteFilePath)) {
File localFile = new File(localFilePath);
if (localFile.exists() && !localFile.delete()) {
throw new IOException("Failed to delete local file: " + localFilePath);
}
if (!localFile.createNewFile()) {
throw new IOException("Failed to create local file: " + localFilePath);
}
int bytesRead;
byte[] buffer = new byte[1024];
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(localFile))) {
while ((bytesRead = outputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
}
}
if (!ftp.completePendingCommand()) {
throw new IOException("Failed to complete FTP command for file: " + remoteFilePath);
}
}
} else if (file.isDirectory()) {
String subRemoteSrcPath = remoteSrcPath + "/" + file.getName();
String subLocalSrcPath = localSrcPath + "/" + file.getName();
String subLocalDestPath = localDestPath + "/" + file.getName();
File subLocalDestDir = new File(subLocalDestPath);
if (!subLocalDestDir.exists() && !subLocalDestDir.mkdir()) {
throw new IOException("Failed to create local subdirectory: " + subLocalDestPath);
}
copyDirectory(subRemoteSrcPath, subLocalSrcPath, subLocalDestPath);
}
}
ftp.changeToParentDirectory();
if (ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
}