<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency> 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 = "ftpuser"; String password = "ftppassword"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } } File localFile = new File("local.txt"); String remoteFile = "remote.txt"; InputStream inputStream = new FileInputStream(localFile); boolean uploaded = ftpClient.storeFile(remoteFile, inputStream); inputStream.close(); if (uploaded) { } else { } String remoteFile = "remote.txt"; File localFile = new File("local.txt"); OutputStream outputStream = new FileOutputStream(localFile); boolean downloaded = ftpClient.retrieveFile(remoteFile, outputStream); outputStream.close(); if (downloaded) { } else { } String remoteFile = "remote.txt"; boolean deleted = ftpClient.deleteFile(remoteFile); if (deleted) { } else { } ftpClient.enterLocalPassiveMode(); ftpClient.enterLocalActiveMode(); ftpClient.setControlEncoding("UTF-8");


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