java在FTP服务器指定下载文件

package com.example.demo.dao;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

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

public class FtpDownload {

    private static final String SERVER = "192.168.8.200";
    private static final int PORT = 21;
    private static final String USER = "root";
    private static final String PASS = "root";
    private static final String REMOTE_DIR = "/2024/06-22/17/00_49_104";
    private static final String LOCAL_BASE_DIR = "C:\\Users\\tang\\Desktop\\test";

    public static void main(String[] args) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(SERVER, PORT);
            ftpClient.login(USER, PASS);
            ftpClient.enterLocalPassiveMode(); // 使用被动模式  

            // 更改远程工作目录到要下载的目录  
            ftpClient.changeWorkingDirectory(REMOTE_DIR);

            // 下载该目录下的所有文件和子目录  
            downloadDirectory(ftpClient, REMOTE_DIR, LOCAL_BASE_DIR);

            ftpClient.logout();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    private static void downloadDirectory(FTPClient ftpClient, String remoteDir, String localDir) throws IOException {
        FTPFile[] files = ftpClient.listFiles(remoteDir);

        if (files != null && files.length > 0) {
            for (FTPFile file : files) {
                String remoteFilePath = remoteDir + "/" + file.getName();
                String localFilePath = localDir + "/" + file.getName();

                if (file.isDirectory()) {
                    // 创建本地目录  
                    new java.io.File(localFilePath).mkdirs();
                    // 递归下载子目录  
                    downloadDirectory(ftpClient, remoteFilePath, localFilePath);
                } else {
                    // 下载文件  
                    OutputStream outputStream = new FileOutputStream(new java.io.File(localFilePath));
                    boolean success = ftpClient.retrieveFile(remoteFilePath, outputStream);
                    outputStream.close();

                    if (success) {
                        System.out.println("File downloaded successfully: " + file.getName());
                    } else {
                        System.out.println("File download failed: " + file.getName());
                    }
                }
            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现FTP下载远程服务器指定目录下所有文件的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.SocketException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FTPDownloadDemo { public static void main(String[] args) { String server = "your.ftp.server.com"; int port = 21; String user = "username"; String password = "password"; String remoteDirPath = "/remote/directory/path"; String localDirPath = "/local/directory/path"; FTPClient ftpClient = new FTPClient(); try { // 连接FTP服务器 ftpClient.connect(server, port); // 登录FTP服务器 ftpClient.login(user, password); // 检查连接和登录是否成功 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); return; } System.out.println("Connected to FTP server."); // 切换到指定目录 ftpClient.changeWorkingDirectory(remoteDirPath); // 获取指定目录下的所有文件 FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.isFile()) { // 下载文件 downloadFile(ftpClient, file.getName(), localDirPath); } } // 登出FTP服务器 ftpClient.logout(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 下载文件 * @param ftpClient FTP客户端 * @param fileName 文件名 * @param localDirPath 本地目录路径 * @throws IOException */ private static void downloadFile(FTPClient ftpClient, String fileName, String localDirPath) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(localDirPath + "/" + fileName); ftpClient.retrieveFile(fileName, fos); } finally { if (fos != null) { fos.close(); } } } } ``` 需要注意的是,上述代码使用了Apache Commons Net库来实现FTP操作,需要在项目中引用该库。可以从Apache官网下载该库或者通过Maven等构建工具添加依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值