SoFunction
Updated on 2025-03-08

SpringBoot's detailed process of using hutool to operate FTP

Project scenario:

<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.9.0</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.15</version>
</dependency>

Implementation steps:

1. Introduce dependencies

<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.9.0</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.15</version>
</dependency>

2. YML configuration

ftp:
  # Server address  host: 127.0.0.1
  # Port number  port: 21
  # username  userName: test
  # password  password: test

3. Config configuration class

I am using static modified variables here to facilitate tool-class calls.

import ;
import ;
/**
  * ftp configuration
  */
@Configuration
public class FtpConfig {
    /**
      * Server address
      */
    private static String host;
    /**
      * Port
      */
    private static Integer port;
    /**
      * username
      */
    private static String userName;
    /**
      * password
      */
    private static String password;
    @Value("${}")
    public void setHost(String host) {
         = host;
    }
    public static String getHost() {
        return host;
    }
    @Value("${}")
    public void setPort(Integer port) {
         = port;
    }
    public static Integer getPort() {
        return port;
    }
    @Value("${}")
    public void setUserName(String userName) {
         = userName;
    }
    public static String getUserName() {
        return userName;
    }
    @Value("${}")
    public void setPassword(String password) {
         = password;
    }
    public static String getPassword() {
        return password;
    }
}

4. FtpUtil tool class

import ;
import ;
import ;
import .slf4j.Slf4j;
import .;
import ;
import ;
import ;
import ;
import ;
/**
  * FTP service tool class
  */
@Slf4j
public class FtpUtil {
    /**
      * Get the FTPClient object
      */
    private static Ftp getFTPClient() {
        try {
            if((()) || () == null
                || (()) || (())) {
                throw new RuntimeException("ftp configuration information cannot be empty");
            }
            Ftp ftp = new Ftp((),(),(),());
            //Set in passive mode to prevent firewall interception            ();
            return ftp;
        } catch (Exception e) {
            ();
            ("Get ftp client exception",e);
            throw new RuntimeException("Get ftp client exception:"+());
        }
    }
    /**
      * Download the file on the ftp server to the local
      * @param remoteFile ftp file path
      * @param localFile The output directory, using the server file name
      */
    public static void download(String remoteFile, String localPath) {
        if((remoteFile) || (localPath)) {
            return;
        }
        Ftp ftp = getFTPClient();
        try {
            if(!(localPath)){
                (localPath);
            }    
            File lFile = (localPath);
            (remoteFile, lFile);
        } catch (Exception e) {
            ();
            ("FTP file download exception",e);
        } finally {
            //Close the connection            try {
                if(ftp != null)  ();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
      * Upload local files to ftp server
      * @param remoteDir uploaded ftp directory
      * @param remoteFileName Name saved to ftp server
      * @param localFile full name of local file
      */
    public static boolean upload(String remoteDir, String remoteFileName, String localFile) {
        if((remoteDir) || (remoteFileName) || (localFile)) {
            return false;
        }
        Ftp ftp = getFTPClient();
        try {
            File lFile = (localFile);
            if(!()) {
                ("The local file does not exist");
                return false;
            }
            if((remoteFileName)) {
                return (remoteDir, lFile);
            } else {
                return (remoteDir, remoteFileName, lFile);
            }
        } catch (Exception e) {
            ();
            ("FTP upload exception",e);
            return false;
        } finally {
            //Close the connection            try {
                if(ftp != null)  ();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
      * Delete files in the FTP server
      * @param remoteFile ftp file path
      */
    public static boolean delFile(String remoteFile) {
        if((remoteFile)) {
            return false;
        }
        Ftp ftp = getFTPClient();
        try {
            return (remoteFile);
        } catch (Exception e) {
            ();
            ("Delete file exception in FTP server",e);
            return false;
        } finally {
            //Close the connection            try {
                if(ftp != null)  ();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
      * It will traverse all files in a directory, and will not traverse recursively.
      * @param path directory
      */
    public static List&lt;String&gt; listFile(String path) {
        List&lt;String&gt; listFile = new ArrayList&lt;&gt;();
        Ftp ftp = getFTPClient();
        try {
            FTPFile[] ftpFiles = (path);
            for (int i = 0; i &lt; ; i++) {
                FTPFile ftpFile = ftpFiles[i];
                if(()){
                    (());
                }
            }
            return listFile;
        } catch (Exception e) {
            ();
            ("Transfer all files in a directory exception",e);
            return null;
        } finally {
            //Close the connection            try {
                if(ftp != null)  ();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

5. Test

@RestController
@RequestMapping("/test")
public class TestController {
	@RequestMapping(value = "ftpTest", method = )
	public void ftpTest() {
		//Upload the file to ftp		("opt/upload","APP_RELATION.sql", "F:/APP_RELATION.sql");
		//Download remote files		("opt/upload/APP_RELATION.sql", "D:/");
		//Delete remote files		("opt/upload/APP_RELATION.sql");
	}
}

Summarize:

I encountered a problem when uploading, that is, when the firewall is turned on locally, the uploaded file size is 0kb, and the firewall must be turned off to be normal. Later, the FTP mode is set to "passive mode" to solve the problem.

//Set in passive mode to prevent firewall interception();

Active Mode:

  • How it works: The client opens an unprivileged port locally (usually greater than 1023) and sends a PORT command to the server through this port, telling the server the port number used for data transmission. The server then uses its port 20 (data port) to actively connect to the port specified by the client for data transmission.
  • Security: This can raise some security issues because the server needs to actively connect to the client's port, especially when the client is behind a firewall or NAT device.
  • Applicable scenarios: Applicable to scenarios where clients are located in network environments where inbound connections are acceptable and there are no firewalls or NAT devices restrictions.

Passive mode:

  • How it works: The client sends a PASV command to the server. The server opens a port locally (usually a high-bit unprivileged port) and tells the client this port number through the response of the PASV command. Then, the client actively connects to the port specified by the server for data transmission.
  • Security: Because clients actively connect to the server, this mode is more suitable for scenarios where clients are located behind firewalls or NAT devices, as these devices usually allow outbound connections but limit inbound connections.
  • Applicable scenarios: Especially suitable for scenarios where the network environment is unstable and the presence of firewalls or NAT devices.

This is the end of this article about SpringBoot using hutool to operate FTP. For more related content on SpringBoot using hutool, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!