2013-04-08 171 views
5

我正在從一個Apache庫(commons.net ver 3.2)的幫助下從服務器上下載ftp文件。下載很好,我收到了我需要的所有文件並將它們保存在一個文件夾中。我遇到的問題是超時,因爲當我下載時連接中斷,我需要一個錯誤消息,告訴我連接已經丟失,但是我發現這樣做有困難,我搜索了無數的論壇,包括這個一個和我已經嘗試了很多方法來解決這個問題,但沒有人有結果呢!那我的代碼如下:apache commons ftp客戶端超時問題

public void doSomething(String ip, int port, String user, String pass, String server, String remotePath, String localPath) { 
    int tenseconds = 10 * 1000; 
    int thirtyseconds = 30 * 3000; 
    Socket s4 = new Socket(); 
    java.net.InetSocketAddress adr = new java.net.InetSocketAddress("213.0.17.234", 21); 
    s4.connect(adr, thirtyseconds); 
    FTPClient client = new FTPClient(); 

    org.apache.commons.vfs2.FileSystemOptions fileSystemOptions = null; 
    String key = FtpFileSystemConfigBuilder.getInstance().getEntryParser(fileSystemOptions); 

    try { 

    client.setConnectTimeout(tenseconds); 
    client.setDefaultTimeout(thirtyseconds); 
    client.connect(ip, port); 
    client.setSoTimeout(thirtyseconds); 

    int reply = client.getReplyCode(); 
    if (!FTPReply.isPositiveCompletion(reply)) { 
     throw new FileSystemException("vfs.provider.ftp/connect-rejected.error"); 
    } 
    client.enterLocalPassiveMode(); 
    boolean login = client.login(user, pass); 

    URL url = new URL("ftp://" + user + ":" + pass + "@" + server + remotePath + ";type=i"); 
    URLConnection urlc = url.openConnection(); 
    urlc.setConnectTimeout(1000); 
    InputStream is = urlc.getInputStream(); 
    BufferedWriter bw = new BufferedWriter(new FileWriter(localPath)); 

    int c; 
    client.setSoTimeout(tenseconds); 

    client.setControlKeepAliveTimeout(10000); 

    while ((c = is.read()) != -1) { 
     urlc.getConnectTimeout(); 

     bw.write(c); 

    } 

    long t2 = System.currentTimeMillis(); 

    System.out.println(t2); 

    JOptionPane.showMessageDialog(null, "se cargo el primer fichero!", "información", JOptionPane.INFORMATION_MESSAGE); 

    if (login) { 

     FTPFile[] files = client.listFiles(); 
     for (FTPFile file : files) { 

     if (file.getType() == FTPFile.DIRECTORY_TYPE) { 

      System.out.println("ftp file: " + file.getName() + ";" + FileUtils.byteCountToDisplaySize(file.getSize())); 

     } else if (file.getType() == FTPFile.FILE_TYPE) { 
      System.out.println("ftp file: " + file.getName() + ";" + FileUtils.byteCountToDisplaySize(file.getSize())); 

     } 

     } 

     is.close(); 
     bw.close(); 

     client.setSoTimeout(tenseconds); 
     client.logout(); 
     client.disconnect(); 

    } 

    } catch (IOException e) { 
    StringWriter sw0 = new StringWriter(); 
    PrintWriter p0 = new PrintWriter(sw0, true); 
    e.printStackTrace(p0); 

    System.out.println("connection probably lost"); 
    JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
    } 
} 

我已經嘗試了所有的東西可以找到一個已經閱讀了setdefaulttimeout用來激活所有的超時,connectiontiomeout來等待連接和getsotimeouts使用當我們下載一個文件但它不起作用時,我已經嘗試了5秒鐘,所以它不會下載文件,但它不起作用,我已經讀過,有一些問題whit connectiontimeouts,我們應該使用socketfactory,所以我也修了一個套接字工廠,我嘗試了,但它不起作用,而且我已經達到了一點,我有點絕望,所以我要求你的幫助,我都嘗試client.setControlKeepAliveTimeout(10000);建立一個活動超時但它沒有工作! :(

+0

'setControlKeepAliveTimeout'的參數以秒爲單位。 – 2013-07-18 15:04:13

回答

0

有你重新連接到服務器與一個URLConnection你已經與FTPClient連接後的任何原因?

FTPClient有retrieveFileStream方法。如果過程中出現錯誤,將拋出一個異常處理