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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章