import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FTPExample { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; FTPClient ftp = new FTPClient(); try { ftp.connect(server, port); ftp.login(username, password); ftp.enterLocalPassiveMode(); ftp.setFileType(FTP.BINARY_FILE_TYPE); String remoteFile = "/path/to/remote/file.txt"; String localFile = "/path/to/local/file.txt"; boolean success = ftp.retrieveFile(remoteFile, new FileOutputStream(localFile)); if (success) { System.out.println("File downloaded successfully!"); } else { System.out.println("Failed to download file."); } ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency>


上一篇:
下一篇:
切换中文