2016-07-08 26 views
1

我想通過FTP使用commons net FTP 3.5和Java 1.8.0.45來重命名這些文件。有一個具有170K小文件(25 GB)的特定文件夾。每當我嘗試列出這個文件夾,它都會返回異常。對於其他文件夾,它運行良好並重命名文件。爲什麼FTPConnectionClosedException是通過FTP爲170K文件的文件夾引發的?

org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication. 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:316) 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:292) 
    at org.apache.commons.net.ftp.FTP.getReply(FTP.java:712) 
    at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1857) 
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3420) 
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3335) 
    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012) 
    at TestFTP.execute(TestFTP.java:27) 
    at TestFTP.main(TestFTP.java:12) 
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication. 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:316) 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:292) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:503) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:628) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:602) 
    at org.apache.commons.net.ftp.FTP.quit(FTP.java:884) 
    at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:1152) 
    at TestFTP.execute(TestFTP.java:62) 
    at TestFTP.main(TestFTP.java:12) 

代碼:

import java.io.IOException; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 

import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 

public class TestFTP { 
    public static void main(String[] args) { 
     TestFTP.execute(args[0], args[1]); 
    } 
    static DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    public static void execute(String ip, String folder) { 
     String server = ip; 
     int port = 21; 
     String user = "adminuser"; 
     String pass = "adminuser"; 

     long start = System.currentTimeMillis(); 
     FTPClient ftpClient = new FTPClient(); 
     try { 
      ftpClient.connect(server, port); 
      ftpClient.login(user, pass); 

      FTPFile[] files = ftpClient.listFiles(folder); 
      for (FTPFile file : files) { 
       String details = file.getName(); 

       // renaming file 
       String oldFile = folder + file.getName(); 
       String newFile = folder + "_X_" + file.getName(); 

       boolean success = ftpClient.rename(oldFile, newFile); 
       if (success) { 
        System.out.println(oldFile + " was successfully renamed to: " 
          + newFile); 
       } else { 
        System.out.println("Failed to rename: " + oldFile); 
       } 
      } 

      ftpClient.logout(); 
      ftpClient.disconnect(); 

      long end = System.currentTimeMillis(); 
      System.out.println("time:" +(end-start)); 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } finally { 
      if (ftpClient.isConnected()) { 
       try { 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

有沒有什麼辦法讓我的FTP服務器響應請求,列出這樣一個大文件夾一樣增加超時?或者我想念什麼?提前致謝 !

+0

你有和Idea多少時間正在做的文件列表? –

+0

握住手機。爲什麼你使用FTP重命名170,000個文件?直接在服務器上執行此操作的效率會更高。我會使用批處理文件或shell腳本,而不是Java代碼。 – EJP

+0

@DanielHernández列表從來沒有成功的這個文件夾。對於大約有100個文件的其他文件夾,大約需要500毫秒。 – bkrish

回答

0

也許您的連接正在關閉,因爲檢索所有信息文件所需的時間應增加超時連接。

你應該超時玩,明智使用它們這些方法從FTPClient:

setDataTimeout(int timeout) //Sets the timeout in milliseconds to use when reading from the data connection. 

setControlKeepAliveTimeout(long controlIdle) 
//Set the time to wait between sending control connection keepalive messages when processing file upload or download. 
+0

我試了兩次超時(日期超時=> 3600秒,ControlKeepAliveTimeout = 0)足夠長。常見的網絡FTP客戶端以相同的方式運行。即使有終端,FTP服務器也會立即關閉帶有錯誤代碼421的連接。(421服務不可用,遠程服務器已關閉連接)。有任何想法嗎 ? – bkrish

相關問題