Java FTP 基本操做

最近工做中用到了 FTP 相關的操做,因此藉此機會瞭解了下具體內容。html

FTP基礎

關於 FTP 基礎推薦閱讀《使用 Socket 通訊實現 FTP 客戶端程序》,其中須要特別注意的是主動模式和被動模式,這一部分在平常使用中常常被忽略,但生產環境中可能會出問題,關鍵在於防火牆對端口的控制。java

  • 主動模式:服務器採用 21 和 20 端口,客戶端採用大於 1024 的隨機端口,鏈接指令和文件傳輸指令由服務端發送。
  • 被動模式:服務端採用 21 和大於 1024 的隨機端口,客戶端採用大於 1024 的隨機端口,鏈接指令由客戶端發送。

程序操做 FTP 過程在上面推薦的文章中有所說起,你們能夠看到過程仍是比較複雜的,不過好在有 apache 的 commons-net 給咱們提供了相關的工具類可使用,本文使用的是 3.6 版本。如下經過代碼進行說明,此代碼僅演示功能,不少地方並不完善,若是用做生產請自行修改。linux

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 變爲 retrieveFileapache

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);
}
相關文章
相關標籤/搜索