【从ftp同步文件到本地】

从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();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值