java ftp详解_Java FTP 基本操作
最近工作中用到了 FTP 相關的操作,所以借此機會了解了下具體內容。
FTP基礎
關于 FTP 基礎推薦閱讀《使用 Socket 通信實現 FTP 客戶端程序》,其中需要特別注意的是主動模式和被動模式,這一部分在日常使用中經常被忽略,但生產環境中可能會出問題,關鍵在于防火墻對端口的控制。
主動模式:服務器采用 21 和 20 端口,客戶端采用大于 1024 的隨機端口,連接指令和文件傳輸指令由服務端發送。
被動模式:服務端采用 21 和大于 1024 的隨機端口,客戶端采用大于 1024 的隨機端口,連接指令由客戶端發送。
程序操作 FTP 過程在上面推薦的文章中有所提及,大家可以看到過程還是比較復雜的,不過好在有 apache 的 commons-net 給我們提供了相關的工具類可以使用,本文使用的是 3.6 版本。以下通過代碼進行說明,此代碼僅演示功能,很多地方并不完善,如果用作生產請自行修改。
Java FTP 上傳
/**
* FTP發送至目標服務器
* @apiNote 依賴apache commons-net 包
* @param server
*/
public static void sendToServerByFTP(String server, int port, String username, String password,
String encoding, String fileLocalPath, String fileRemotePath, String fileRemoteName) throws IOException {
// 獲取 FTPClient
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("connected failed");
}
// 設置編碼,當文件中存在中文且上傳后文件亂碼時可使用此配置項
//ftpClient.setControlEncoding(encoding);
// 切換為本地被動模式,可以解決FTP上傳后文件為空的問題,但需要服務器將FTP服務添加至防火墻白名單
//ftpClient.enterLocalPassiveMode();
// 切換到指定目錄
ftpClient.changeWorkingDirectory(fileRemotePath);
// 獲取文件并上傳
File file = new File(fileLocalPath);
InputStream inputStream = new FileInputStream(file);
//文件名為中文名且上傳后出現亂碼時啟用此項
//String fileName = new String(fileRemoteName.getBytes(encoding), "ISO8859-1");
boolean flag = ftpClient.storeFile(fileRemoteName, inputStream);
// 關閉已占用資源
inputStream.close();
ftpClient.logout();
}
FTP 下載
FTP 下載和上傳基本步驟類似,依賴的方法由 storeFile 變為 retrieveFile
public void downloadFile(String server, int port, String username, String password,
String serverPath, String localPath, String fileName) throws IOException {
// 登錄
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 驗證登錄情況
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
throw new RuntimeException("登錄FTP服務器失敗,錯誤代碼:" + replyCode);
}
// 切換服務器至目標目錄
ftpClient.changeWorkingDirectory(serverPath);
// 下載文件
File file = new File(localPath);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ftpClient.retrieveFile(fileName, fileOutputStream);
// 關閉資源占用
fileOutputStream.close();
ftpClient.logout();
}
FTP 刪除
public void deleteFile(String server, int port, String username, String password,
String serverPath, String fileName) throws IOException {
// 登錄
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(username, password);
// 驗證登錄情況
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
throw new RuntimeException("登錄FTP服務器失敗,錯誤代碼:" + replyCode);
}
ftpClient.changeWorkingDirectory(serverPath);
ftpClient.deleteFile(fileName);
}
總結
以上是生活随笔為你收集整理的java ftp详解_Java FTP 基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fundamentals of Comp
- 下一篇: 人脸爬取(人脸数据集的搜集)