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 = "your-username"; String password = "your-password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); String remoteFile = "/path/to/remote/file.txt"; File localFile = new File("path/to/local/file.txt"); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); ftpClient.retrieveFile(remoteFile, outputStream); outputStream.close(); ftpClient.storeFile(remoteFile, new FileInputStream(localFile)); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } } <dependencies> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency> </dependencies>


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