以下是一个基本的 FTP 工具类,包含上传文件、下载文件、删除文件等方法:

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

import java.io.*;

public class FtpUtil {

    private FTPClient ftpClient;

    public FtpUtil() {
        ftpClient = new FTPClient();
    }

    /**
     * 连接 FTP 服务器
     * @param host 服务器主机名
     * @param port 服务器端口号
     * @param username 登录用户名
     * @param password 登录密码
     * @return 是否连接成功
     */
    public boolean connect(String host, int port, String username, String password) {
        try {
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 断开与 FTP 服务器的连接
     */
    public void disconnect() {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 上传文件到 FTP 服务器
     * @param localPath 本地文件路径
     * @param remotePath FTP 服务器文件路径
     * @return 是否上传成功
     */
    public boolean uploadFile(String localPath, String remotePath) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(localPath));
            return ftpClient.storeFile(remotePath, inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 下载 FTP 服务器上的文件
     * @param remotePath FTP 服务器文件路径
     * @param localPath 本地文件路径
     * @return 是否下载成功
     */
    public boolean downloadFile(String remotePath, String localPath) {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File(localPath));
            return ftpClient.retrieveFile(remotePath, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除 FTP 服务器上的文件
     * @param remotePath FTP 服务器文件路径
     * @return 是否删除成功
     */
    public boolean deleteFile(String remotePath) {
        try {
            return ftpClient.deleteFile(remotePath);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断 FTP 服务器上的文件是否存在
     * @param remotePath FTP 服务器文件路径
     * @return 是否存在
     */
    public boolean existFile(String remotePath) {
        try {
            FTPFile[] files = ftpClient.listFiles(remotePath);
            return files.length > 0;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}

这个工具类使用了 Apache Commons Net 库来操作 FTP 服务器,需要在项目中引入该库。使用示例:

public static void main(String[] args) {
    FtpUtil ftpUtil = new FtpUtil();
    if (ftpUtil.connect('localhost', 21, 'username', 'password')) {
        // 上传文件
        ftpUtil.uploadFile('C:/test.txt', '/test.txt');
        // 下载文件
        ftpUtil.downloadFile('/test.txt', 'C:/test_download.txt');
        // 删除文件
        ftpUtil.deleteFile('/test.txt');
        // 判断文件是否存在
        ftpUtil.existFile('/test.txt');
        ftpUtil.disconnect();
    }
}
Java FTP 工具类:上传、下载、删除文件 (JDK 1.8)

原文地址: https://www.cveoy.top/t/topic/n49q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录