import org.apache.commons.net.ftp.*; public class FTPDownloader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String remoteFilePath = "/path/to/remote/file"; String localFilePath = "/path/to/local/file"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFilePath)); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); ftpClient.logout(); ftpClient.disconnect(); System.out.println("File downloaded successfully."); } catch (IOException e) { e.printStackTrace(); } } } import com.enterprisedt.net.ftp.*; public class FTPUploader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String localFilePath = "/path/to/local/file"; String remoteFilePath = "/path/to/remote/file"; FTPClient ftpClient = new FTPClient(); try { ftpClient.setRemoteHost(server); ftpClient.setRemotePort(port); ftpClient.setUserName(username); ftpClient.setPassword(password); ftpClient.connect(); ftpClient.uploadFile(localFilePath, remoteFilePath); ftpClient.quit(); System.out.println("File uploaded successfully."); e.printStackTrace(); } } } <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency>


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