java ftp upload file related code sorting

Posted by roel_v on Sat, 08 Jan 2022 06:45:12 +0100

ftp file upload using java

1. ftp file upload steps

1) Connect ftp
2) Check whether the working directory exists
3) Check whether the upload is successful
4) After completing the task, disconnect ftp

2. Specifically, paste the core code directly as follows:

A) Import core pom dependencies
		<!--FTP package-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.5</version>
        </dependency>
        <!--Log package-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.9</version>
        </dependency>
B) Paste the FTPTools tool class directly here
package com.example.demo.ftp;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;

import java.io.IOException;
import java.io.InputStream;

/**
 * Upload files via FTP
 *
 * @Author zhengwd, Refer to lvhaibao
 * @Date 2021/12/21
 */
@Slf4j
public class FTPTools {

    /**
     * Setting private cannot be instantiated
     */
    private FTPTools() {}

    /**
     * upload
     *
     * @param hostname
     * @param port
     * @param username
     * @param password
     * @param workingPath Working directory of the server
     * @param inputStream Input stream of file
     * @param saveName    File name to save
     * @return
     */
    public static boolean upload(String hostname, int port, String username, String password, String workingPath, InputStream inputStream, String saveName) {
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        // 1. Connect ftp
        if (connect(ftpClient, hostname, port, username, password)) {
            try {
                // 2. Check whether the working directory exists
                if (ftpClient.changeWorkingDirectory(workingPath)) {
                    // 3. Check whether the upload is successful
                    if (storeFile(ftpClient, saveName, inputStream)) {
                        flag = true;
                        // 4. Complete the task and disconnect ftp
                        disconnect(ftpClient);
                    }
                }
            } catch (IOException e) {
                log.error("The working directory does not exist");
                e.printStackTrace();
                // 4. Complete the task and disconnect ftp
                disconnect(ftpClient);
            }
        }
        return flag;
    }

    /**
     * Disconnect
     *
     * @param ftpClient
     * @throws Exception
     */
    public static void disconnect(FTPClient ftpClient) {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
                log.error("Connection closed");
            } catch (IOException e) {
                log.error("The connection was not closed");
                e.printStackTrace();
            }
        }
    }

    /**
     * Test whether it can be connected
     *
     * @param ftpClient
     * @param hostname  ip Domain name or address
     * @param port      port
     * @param username  user name
     * @param password  password
     * @return Return true to connect
     */
    public static boolean connect(FTPClient ftpClient, String hostname, int port, String username, String password) {
        boolean flag = false;
        try {
            ftpClient.connect(hostname, port);

            // The author of the original article placed the coding format here. When I actually use it,
            //  There is no problem with your own ftp server. It may be locally compatible with utf-8, but the remote (other people's) server will have color garbled code
//            ftpClient.enterLocalPassiveMode();
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            ftpClient.setControlEncoding("UTF-8");

            if (ftpClient.login(username, password)) {
                // After checking some articles, you need to set the encoding format after ftp login is successful, otherwise the encoding format is invalid, and the file content may be damaged, such as color garbled code
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.setControlEncoding("UTF-8");

                log.info("connect ftp success");
                flag = true;
            } else {
                log.error("connect ftp Failed. The user name or password may be wrong");
                try {
                    disconnect(ftpClient);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            log.error("Connection failed, possibly ip Or port error");
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * Upload file
     *
     * @param ftpClient
     * @param saveName        Full path. For example, / home/public/a.txt
     * @param fileInputStream Input file stream
     * @return
     */
    public static boolean storeFile(FTPClient ftpClient, String saveName, InputStream fileInputStream) {
        boolean flag = false;
        try {
            if (ftpClient.storeFile(saveName, fileInputStream)) {
                flag = true;
                log.error("Upload succeeded");
                disconnect(ftpClient);
            }
        } catch (IOException e) {
            log.error("Upload failed");
            disconnect(ftpClient);
            e.printStackTrace();
        }
        return flag;
    }
}
C) Test class code
package com.example.demo.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Author zhengwd, Refer to lvhaibao
 * @Date 2021/12/21
 */
public class MainTest {

    public static void main(String[] args) throws FileNotFoundException {
        // Server IP
        String hostname = "";
        // Port number
        int port = 21;
        // Login user name
        String username = "";
        // Login password
        String password = "";
        // File transfer folder path
        String workingPath = "/";

        String str = "C:\\Users\\sourceFileName.pdf";
        InputStream fileInputStream = new FileInputStream(new File(str));
        String saveName = "targetFileName.pdf";
        System.out.println(FTPTools.upload( hostname, port, username, password, workingPath, fileInputStream, saveName));
    }
}

Test results:

extend

ftp access mode

ftp://ftp server ip

If identity verification is required, enter the account password for verification

At this time, you can see that the file is uploaded to the specified location of the specified ftp server, as follows:

Some people may not have ftp server related information. Please refer to the following reference article 2 to build one yourself!

The PDF file is damaged and color is garbled

It's OK to transfer PDF files to your own ftp server in the way of "reference article 1". It may be locally compatible with utf-8, but the remote (other people's) server will appear color garbled code, such as this:

I knew it was a coding problem, but I didn't know how to change it. It was hard at that time!

Later, I found "reference article 3" and found that there was a problem with the position of the character coding setting. I hadn't seen this line of words before, and took many detours, as follows:

Specific optimized code content of the original code:

Just try to find it again. Be happy to take off~~~

Code optimization scheme when batch processing is required

		FTPClient ftpClient = new FTPClient();
        // 1. Connect ftp
        boolean connect = FTPTools.connect(ftpClient, hostName, port, userName, password);
        if (connect) {
            try {
                // 2 check whether the working directory exists
                boolean isExist = ftpClient.changeWorkingDirectory(workingPath);
                if (isExist) {
                    // TODO batch data code data acquisition
                    List<Map<String, Object>> list = new ArrayList<>();
                    int dataCount = list.size();
                    for (int i = 0; i < dataCount; i++) {
                        // TODO stream acquisition
                        InputStream fileInputStream = null;
                        String fileFtpName = "";

                        try {
                            // 3. Check whether the upload is successful
                            boolean upload = FTPTools.storeFileByConnOne(ftpClient, fileFtpName, fileInputStream);
                            log.info("Come in, serial number:{}´╝îresult:{}", i, upload);
                            // Subsequent operations after successful upload of TODO


                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        } finally {
                            // Close the file stream
                            if (fileInputStream != null) {
                                fileInputStream.close();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                log.error("The working directory does not exist");
                e.printStackTrace();
            } finally {
                // Close ftp connection
                FTPTools.disconnect(ftpClient);
            }
        }

FTPTools adds a new method and removes the method of disconnecting ftp, as follows:

    public static boolean storeFileByConnOne(FTPClient ftpClient, String saveName, InputStream fileInputStream) {
        boolean flag = false;
        try {
            if (ftpClient.storeFile(saveName, fileInputStream)) {
                flag = true;
                log.error("Upload succeeded");
//                disconnect(ftpClient);
            }
        } catch (IOException e) {
            log.error("Upload failed");
//            disconnect(ftpClient);
            e.printStackTrace();
        }
        return flag;
    }

In this way, batch upload can be realized (connect ftp once, transmit all the time, and finally disconnect, so as to save the cost of frequent connection)

Reference article:
Java uses FTP to upload files
Quickly build a local FTP server
Solutions to problems related to JAVA FTPClient (slow speed, Chinese garbled code, content loss)

Topics: Java ftp