Java FTP基本操作

最近工作中用到了 FTP 相關(guān)的操作,所以借此機(jī)會了解了下具體內(nèi)容。

FTP基礎(chǔ)

關(guān)于 FTP 基礎(chǔ)推薦閱讀《使用 Socket 通信實(shí)現(xiàn) FTP 客戶端程序》,其中需要特別注意的是主動模式和被動模式,這一部分在日常使用中經(jīng)常被忽略,但生產(chǎn)環(huán)境中可能會出問題,關(guān)鍵在于防火墻對端口的控制。

  • 主動模式:服務(wù)器采用 21 和 20 端口,客戶端采用大于 1024 的隨機(jī)端口,連接指令和文件傳輸指令由服務(wù)端發(fā)送。
  • 被動模式:服務(wù)端采用 21 和大于 1024 的隨機(jī)端口,客戶端采用大于 1024 的隨機(jī)端口,連接指令由客戶端發(fā)送。

程序操作 FTP 過程在上面推薦的文章中有所提及,大家可以看到過程還是比較復(fù)雜的,不過好在有 apache 的 commons-net 給我們提供了相關(guān)的工具類可以使用,本文使用的是 3.6 版本。以下通過代碼進(jìn)行說明,此代碼僅演示功能,很多地方并不完善,如果用作生產(chǎn)請自行修改。

Java FTP 上傳

/**
 * FTP發(fā)送至目標(biāo)服務(wù)器
 * @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");
    }

    // 設(shè)置編碼,當(dāng)文件中存在中文且上傳后文件亂碼時(shí)可使用此配置項(xiàng)
    //ftpClient.setControlEncoding(encoding);
    // 切換為本地被動模式,可以解決FTP上傳后文件為空的問題,但需要服務(wù)器將FTP服務(wù)添加至防火墻白名單
    //ftpClient.enterLocalPassiveMode();

    // 切換到指定目錄
    ftpClient.changeWorkingDirectory(fileRemotePath);

    // 獲取文件并上傳
    File file = new File(fileLocalPath);
    InputStream inputStream = new FileInputStream(file);

    //文件名為中文名且上傳后出現(xiàn)亂碼時(shí)啟用此項(xiàng)
    //String fileName = new String(fileRemoteName.getBytes(encoding), "ISO8859-1");
    boolean flag = ftpClient.storeFile(fileRemoteName, inputStream);

    // 關(guān)閉已占用資源
    inputStream.close();
    ftpClient.logout();
}

FTP 下載

FTP 下載和上傳基本步驟類似,依賴的方法由 storeFile 變?yōu)?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);

    // 驗(yàn)證登錄情況
    int replyCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        throw new RuntimeException("登錄FTP服務(wù)器失敗,錯(cuò)誤代碼:" + replyCode);
    }
    
    // 切換服務(wù)器至目標(biāo)目錄
    ftpClient.changeWorkingDirectory(serverPath);

    // 下載文件
    File file = new File(localPath);
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    ftpClient.retrieveFile(fileName, fileOutputStream);
    
    // 關(guān)閉資源占用
    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);

    // 驗(yàn)證登錄情況
    int replyCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        throw new RuntimeException("登錄FTP服務(wù)器失敗,錯(cuò)誤代碼:" + replyCode);
    }

    ftpClient.changeWorkingDirectory(serverPath);

    ftpClient.deleteFile(fileName);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容