JAVA中使用FTPClient上传下载

本文介绍如何使用Jakarta Commons中的FTPClient进行FTP文件上传和下载操作。通过简单的示例代码展示了如何连接FTP服务器、上传及下载文件,并提供了必要的Maven依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件,我创建的是mvn项目,所以在pom.xml文件中添加依赖:
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/oro/oro -->
<dependency>
    <groupId>oro</groupId>
    <artifactId>oro</artifactId>
    <version>2.0.8</version>
</dependency>

自己写代码前可以先看下官方的API,官网写的还是很详细的(http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html),简单截个图看下:

好了,简单写个例子,很简单,一看就明白了
/**
 * 向FTP服务器上传文件
 *
 * @param url        FTP服务器url
 * @param port       FTP服务器端口
 * @param username
 * @param password
 * @param path       本地文件目录
 * @param remoteFile FTP服务器保存文件名
 */
public void sendToServer(String url, int port, String username, String password,
                         String path, String remoteFile) {
    //创建ftp客户端
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("GBK");
    try {
        //链接ftp服务器
        ftpClient.connect(url, port);
        //登录ftp
        ftpClient.login(username, password);
        int reply = ftpClient.getReplyCode();
        System.out.println(reply);
        //如果reply返回230就算成功了,如果返回530密码用户名错误或当前用户无权限下面有详细的解释。
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return;
        }
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

        ftpClient.changeWorkingDirectory("/data");//修改工作目录
        File file = new File(path);
        InputStream input = new FileInputStream(file);
        ftpClient.storeFile(remoteFile, input);//文件名若是不指定就会上传到root目录下
        input.close();
        ftpClient.logout();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

    }

}

/**
 * Description: 从FTP服务器下载文件
 *
 * @param remotePath FTP服务器上的相对路径
 * @param fileName   要下载的文件名
 * @param localPath  下载后保存到本地的路径
 */
public static boolean download(String url, int port, String username, String password,
                               String remotePath, String fileName, String localPath) {
    boolean success = false;
    //创建ftp客户端
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("GBK");
    try {
        int reply;
        ftpClient.connect(url, port);
        //如果采用默认端口,可以使用ftpClient.connect(url)的方式直接连接FTP服务器
        ftpClient.login(username, password);//登录
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return false;
        }
        ftpClient.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
        FTPFile[] fs = ftpClient.listFiles();
        for (FTPFile ff : fs) {
            if (ff.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + ff.getName());

                OutputStream is = new FileOutputStream(localFile);
                ftpClient.retrieveFile(ff.getName(), is);
                is.close();
            }
        }

        ftpClient.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

参考:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值