2013-04-08 57 views
5

Apacheライブラリ(commons.net ver 3.2)の助けを借りて、サーバーからftpファイルをダウンロードしています。ダウンロードは正常だったし、必要なファイルをすべて受信してフォルダに保存した。私が持っている問題はタイムアウトです。なぜなら、ダウンロード中に接続が中断されたとき、接続が失われていることを示すエラーメッセージが必要ですが、これを行うことが困難であることがわかりました。私はこれを解決するために多くの方法を試しましたが、誰もまだ結果を持っていません!私はsetdefaulttimeoutは、すべてのタイムアウトを活性化するために使用されていることを読んだことがある見つけることができるすべてのものを試してみました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); 
    } 
} 

、connectiontiomeoutが使用されている接続とgetsotimeoutsを待つために使用されている次のように私が持っているコードです。私たちがファイルをダウンロードしても動作しません。私は5秒間それを試してみましたが、ファイルをダウンロードしませんが、動作しません、私はいくつかの問題が接続タイムアウトと、だから私もソケット工場を壊してしまいましたが、動作しませんでした。私は少し不満を感じています。だから助けてもらおうと、client.setControlKeepAliveTimeout(10000);を試してみましたが、それは動作しませんでした! :(

+0

'setControlKeepAliveTimeout'のパラメータは秒単位です。 –

答えて

0

は、すでにFTPClientに接続した後、あなたがURLConnectionのにサーバーに再接続している任意の理由は?

FTPClientがretrieveFileStreamメソッドを持っているがあります。中にエラーが発生した場合、これは例外がスローされます

関連する問題