FtpUtil.java 8.8 KB
package com.aigeo.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class FtpUtil {

    // FTP服务器配置参数
    private static final String SERVER = "193.112.177.8"; // FTP服务器地址
    private static final int PORT = 21;                     // FTP服务器端口,默认21
    private static final String USERNAME = "user1"; // FTP用户名
    private static final String PASSWORD = "sz123321"; // FTP密码

    // 本地文件路径及远程存储路径
    private static final String LOCAL_FILE_PATH = "C:\\Users\\Lenovo\\Desktop\\sql.txt";
    private static final String REMOTE_FILE_PATH = "/1/sql.txt";

//    public static void upload(FtpDTO ftpDTO) {
//        FTPClient ftpClient = new FTPClient();
//        try {
//            // 1. 连接FTP服务器
//            System.out.println("正在连接到FTP服务器 " + ftpDTO.getFtpHost() + "...");
//            ftpClient.connect(ftpDTO.getFtpHost(), PORT);
//            int replyCode = ftpClient.getReplyCode();
//            if (!FTPReply.isPositiveCompletion(replyCode)) {
//                System.err.println("FTP服务器拒绝连接,响应代码:" + replyCode);
//                return;
//            }
//            System.out.println("连接成功,准备登录...");
//
//            // 2. 登录FTP服务器
//            boolean success = ftpClient.login(ftpDTO.getUser(), ftpDTO.getPassword());
//            if (!success) {
//                System.err.println("FTP登录失败,请检查用户名和密码。");
//                return;
//            }
//            System.out.println("登录成功!");
//
//            // 3. 设置FTP客户端工作模式
//            // 进入被动模式,以适应防火墙/NAT环境
//            ftpClient.enterLocalPassiveMode();
//            // 设置文件传输类型为二进制文件,确保上传文件内容正确
//            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//
//            // 4. 上传文件
//            System.out.println("开始上传文件:" + ftpDTO.getSourcePath());
//            // 使用try-with-resources确保InputStream自动关闭
//            try (InputStream inputStream = new FileInputStream(ftpDTO.getSourcePath())) {
//                boolean uploadSuccess = ftpClient.storeFile(ftpDTO.getTargetPath(), inputStream);
//                if (uploadSuccess) {
//                    System.out.println("文件上传成功!保存到:" + ftpDTO.getTargetPath());
//                } else {
//                    System.err.println("文件上传失败,请检查文件路径和网络连接。");
//                }
//            }
//
//            // 5. 登出并断开FTP连接
//            ftpClient.logout();
//            System.out.println("已登出FTP服务器。");
//
//        } catch (IOException ex) {
//            System.err.println("发生异常:" + ex.getMessage());
//            ex.printStackTrace();
//        } finally {
//            // 6. 确保FTP连接被断开
//            if (ftpClient.isConnected()) {
//                try {
//                    ftpClient.disconnect();
//                    System.out.println("FTP连接已关闭。");
//                } catch (IOException ex) {
//                    System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
//                    ex.printStackTrace();
//                }
//            }
//        }
//    }

//    public static List<String> listFileNames(ApiConfig apiConfig, String directoryPath) {
//        FTPClient ftpClient = new FTPClient();
//        List<String> fileNames = new ArrayList<>();
//
//        try {
//            // 连接并登录FTP服务器
//            ftpClient.connect(apiConfig.getApiUrl(), PORT);
//            int replyCode = ftpClient.getReplyCode();
//            if (!FTPReply.isPositiveCompletion(replyCode)) {
//                return null;
//            }
//
//            boolean success = ftpClient.login(apiConfig.getApiId(), apiConfig.getApiKey());
//            if (!success) {
//                return null;
//            }
//
//            // 进入被动模式
//            ftpClient.enterLocalPassiveMode();
//
//            // 获取文件列表
//            FTPFile[] files;
//            if (directoryPath != null && !directoryPath.isEmpty()) {
//                files = ftpClient.listFiles(directoryPath);
//            } else {
//                files = ftpClient.listFiles();
//            }
//
//            // 提取文件名称
//            for (FTPFile file : files) {
//                fileNames.add(file.getName());
//            }
//
//            return fileNames;
//
//        } catch (IOException ex) {
//            return null;
//        } finally {
//            // 确保连接被正确关闭
//            if (ftpClient.isConnected()) {
//                try {
//                    ftpClient.logout();
//                    ftpClient.disconnect();
//                } catch (IOException ex) {
//                    System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
//                    ex.printStackTrace();
//                }
//            }
//        }
//    }

    /**
     * 获取FTP服务器当前目录下的文件列表
     * @return FTPFile数组,包含目录中的文件和子目录信息
     */
//    public static FTPFile[] getlistFiles() {
//        return listFiles(null);
//    }

    /**
     * 打印指定目录下的文件列表信息
     * @param directoryPath 目录路径
     */
//    public static void printFileList(String directoryPath) {
//        FTPFile[] files = listFiles(directoryPath);
//
//        if (files.length == 0) {
//            System.out.println("目录为空或获取失败");
//            return;
//        }
//
//        System.out.println("目录 " + (directoryPath != null ? directoryPath : "当前目录") + " 中的文件列表:");
//        for (FTPFile file : files) {
//            String fileType = file.isDirectory() ? "目录" : "文件";
//            System.out.printf("%-10s %-20s %10d bytes%n",
//                    fileType, file.getName(), file.getSize());
//        }
//    }


//    public static byte[] downloadFile(ApiConfig apiConfig, String targetPath) {
//        FTPClient ftpClient = new FTPClient();
//        try {
//            // 1. 连接FTP服务器
//            System.out.println("正在连接到FTP服务器 " + apiConfig.getApiUrl() + "...");
//            ftpClient.connect(apiConfig.getApiUrl(), PORT);
//            int replyCode = ftpClient.getReplyCode();
//            if (!FTPReply.isPositiveCompletion(replyCode)) {
//                System.err.println("FTP服务器拒绝连接,响应代码:" + replyCode);
//                return null;
//            }
//            System.out.println("连接成功,准备登录...");
//
//            // 2. 登录FTP服务器
//            boolean success = ftpClient.login(apiConfig.getApiId(), apiConfig.getApiKey());
//            if (!success) {
//                System.err.println("FTP登录失败,请检查用户名和密码。");
//                return null;
//            }
//            System.out.println("登录成功!");
//
//            // 3. 设置FTP客户端工作模式
//            // 进入被动模式,以适应防火墙/NAT环境
//            ftpClient.enterLocalPassiveMode();
//            // 设置文件传输类型为二进制文件,确保下载文件内容正确
//            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//
//            // 4. 下载文件
//            System.out.println("开始下载文件:" + targetPath);
//            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//            boolean downloadSuccess = ftpClient.retrieveFile(targetPath, outputStream);
//            if (downloadSuccess) {
//                System.out.println("文件下载成功!文件大小:" + outputStream.size() + " 字节");
//                return outputStream.toByteArray();
//            } else {
//                System.err.println("文件下载失败,请检查文件路径和网络连接。");
//                return null;
//            }
//
//        } catch (IOException ex) {
//            System.err.println("下载文件时发生异常:" + ex.getMessage());
//            ex.printStackTrace();
//            return null;
//        } finally {
//            // 5. 确保FTP连接被正确关闭
//            if (ftpClient.isConnected()) {
//                try {
//                    ftpClient.logout();
//                    ftpClient.disconnect();
//                    System.out.println("FTP连接已关闭。");
//                } catch (IOException ex) {
//                    System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
//                    ex.printStackTrace();
//                }
//            }
//        }
//    }
}