Using FTPClient to upload and download in JAVA

In JAVA programs, you often need to deal with FTP, such as uploading files to FTP server and downloading files. This paper briefly introduces how to use FTPClient (in commons net package) in jakarta commons to upload and download files. I created mvn project, so I added dependency in pom.xml file:
<!-- 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>

Before you write your own code, you can read the official API first. What you write on the official website is very detailed( http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html )See the following picture for a simple section:

OK, let's write an example. It's very simple. You can understand it at a glance
/**
 * Upload files to FTP server
 *
 * @param url        FTP Server url
 * @param port       FTP Server port
 * @param username
 * @param password
 * @param path       Local file directory
 * @param remoteFile FTP Server save filename
 */
public void sendToServer(String url, int port, String username, String password,
                         String path, String remoteFile) {
    //Create ftp client
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("GBK");
    try {
        //Link ftp server
        ftpClient.connect(url, port);
        //Login ftp
        ftpClient.login(username, password);
        int reply = ftpClient.getReplyCode();
        System.out.println(reply);
        //If reply returns 230, it will succeed. If 530 password is returned, the user name is wrong or the current user does not have permission, there is a detailed explanation below.
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return;
        }
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

        ftpClient.changeWorkingDirectory("/data");//Modify working directory
        File file = new File(path);
        InputStream input = new FileInputStream(file);
        ftpClient.storeFile(remoteFile, input);//If the file name is not specified, it will be uploaded to the root directory
        input.close();
        ftpClient.logout();

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

    }

}

/**
 * Description: Download files from FTP server
 *
 * @param remotePath FTP Relative path on server
 * @param fileName   File name to download
 * @param localPath  Path saved to local after download
 */
public static boolean download(String url, int port, String username, String password,
                               String remotePath, String fileName, String localPath) {
    boolean success = false;
    //Create ftp client
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("GBK");
    try {
        int reply;
        ftpClient.connect(url, port);
        //If the default port is used, you can directly connect to the FTP server by means of ftpClient.connect(url)
        ftpClient.login(username, password);//Sign in
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return false;
        }
        ftpClient.changeWorkingDirectory(remotePath);//Transfer to FTP server directory
        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;
}

Reference resources:

Tags: Java ftp Apache xml

Posted on Sun, 01 Dec 2019 07:00:00 -0800 by goochy808