FTP文件同步

327 阅读1分钟

在Spring Boot中,可以使用Apache Commons Net库来实现FTP文件同步

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@SpringBootApplication
public class FtpSyncApplication implements CommandLineRunner {

    private final String localFolderPath = "path/to/local/folder";
    private final String remoteFolderPath = "path/to/remote/folder";
    private final String ftpServer = "ftp.example.com";
    private final int ftpPort = 21;
    private final String ftpUser = "username";
    private final String ftpPass = "password";

    public static void main(String[] args) {
        SpringApplication.run(FtpSyncApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ftpServer, ftpPort);
            ftpClient.login(ftpUser, ftpPass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            // 获取远程文件夹中的文件列表
            FTPFile[] remoteFiles = ftpClient.listFiles(remoteFolderPath);

            // 遍历远程文件夹中的文件
            for (FTPFile remoteFile : remoteFiles) {
                String remoteFileName = remoteFile.getName();
                String localFilePath = localFolderPath + File.separator + remoteFileName;
                
                // 如果本地文件夹中不存在该文件,则从服务器下载到本地
                if (!new File(localFilePath).exists()) {
                    File localFile = new File(localFilePath);
                    OutputStream outputStream = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(remoteFolderPath + File.separator + remoteFileName, outputStream);
                    outputStream.close();
                    System.out.println("文件下载成功:" + localFilePath);
                }
            }

            // 遍历本地文件夹中的文件
            File localFolder = new File(localFolderPath);
            File[] localFiles = localFolder.listFiles();
            for (File localFile : localFiles) {
                String localFileName = localFile.getName();
                String remoteFilePath = remoteFolderPath + File.separator + localFileName;
                
                // 如果远程文件夹中不存在该文件,则上传到服务器
                if (!existFile(ftpClient, remoteFilePath)) {
                    ftpClient.storeFile(remoteFilePath, new FileInputStream(localFile));
                    System.out.println("文件上传成功:" + remoteFilePath);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 判断远程文件夹中是否存在某个文件
    private boolean existFile(FTPClient ftpClient, String filePath) throws IOException {
        FTPFile[] files = ftpClient.listFiles(filePath);
        return files.length > 0;
    }
}

首先创建一个FTPClient对象,并连接到FTP服务器。然后,通过listFiles方法获取远程文件夹中的文件列表,遍历每个文件,如果本地文件夹中不存在该文件,则从服务器下载到本地。然后,遍历本地文件夹中的文件,如果远程文件夹中不存在该文件,则将其上传到服务器。最后,关闭FTP连接。

请注意,需要将代码中的localFolderPathremoteFolderPathftpServerftpPortftpUserftpPass替换为实际的值。此外,还需要添加commons-net依赖项到您的pom.xml文件中:

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.7.2</version>
</dependency>