Java FTP下载示例– Apache Commons Net

本文介绍了一个使用Apache Commons Net API的Java FTP下载文件示例。文章详细解释了如何创建FTP连接,并使用downloadFile()方法从FTP服务器下载文件到本地系统。

Today we will look into Java FTP download file example using Apache Commons Net API. Few days back I wrote a post on how to FTP Upload File using Apache Commons Net API. Here we will learn how to use apache commons Net API to download file from FTP server.

今天,我们将研究使用Apache Commons Net API的Java FTP下载文件示例。 几天前,我写了一篇有关如何使用Apache Commons Net API通过FTP上传文件的文章。 在这里,我们将学习如何使用apache commons Net API从FTP服务器下载文件。

Java FTP下载 (Java FTP Download)

package com.journaldev.files;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPDownloader {

    FTPClient ftp = null;

    public FTPDownloader(String host, String user, String pwd) throws Exception {
        ftp = new FTPClient();
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftp.connect(host);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user, pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    }

    public void downloadFile(String remoteFilePath, String localFilePath) {
        try (FileOutputStream fos = new FileOutputStream(localFilePath)) {
            this.ftp.retrieveFile(remoteFilePath, fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        if (this.ftp.isConnected()) {
            try {
                this.ftp.logout();
                this.ftp.disconnect();
            } catch (IOException f) {
                // do nothing as file is already downloaded from FTP server
            }
        }
    }

    public static void main(String[] args) {
        try {
            FTPDownloader ftpDownloader =
                new FTPDownloader("ftp_server.journaldev.com", "ftp_user@journaldev.com", "ftpPassword");
            ftpDownloader.downloadFile("sitemap.xml", "/Users/pankaj/tmp/sitemap.xml");
            System.out.println("FTP File downloaded successfully");
            ftpDownloader.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In above program constructor, we are creating FTP connection and then using downloadFile() method to download the file located on FTP server to local system. FTPClient retrieveFile() method is used to download file from FTP server.

在上面的程序构造函数中,我们创建FTP连接,然后使用downloadFile()方法将FTP服务器上的文件下载到本地系统。 FTPClient resolveFile retrieveFile()方法用于从FTP服务器下载文件。

Note that the remote file path should be relative to the FTP user home directory.

请注意,远程文件路径应相对于FTP用户主目录。

Here is the output of the above FTP download file example program.

这是上述FTP下载文件示例程序的输出。

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 01:39. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 15 minutes of inactivity.
USER ftp_user@journaldev.com
331 User ftp_user@journaldev.com OK. Password required
PASS ftpPassword
230 OK. Current restricted directory is /
TYPE I
200 TYPE is now 8-bit binary
PASV
227 Entering Passive Mode (50,116,65,161,255,56)
RETR sitemap.xml
150-Accepted data connection
150 1427.4 kbytes to download
226-File successfully transferred
226 1.900 seconds (measured here), 0.73 Mbytes per second
FTP File downloaded successfully
QUIT
221-Goodbye. You uploaded 0 and downloaded 1428 kbytes.
221 Logout.

That’s all for FTP download file example using Apache commons Net API.

这就是使用Apache commons Net API的FTP下载文件示例的全部内容。

翻译自: https://www.journaldev.com/974/java-ftp-download-apache-commons-net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值