org.apache.commons.net.ftp挂起超时

时间:2014-09-17 13:12:28

标签: upload timeout

我在使用org.apache.commons.net.ftp时遇到了麻烦。 我的客户端需要在上传时检测超时(下载超时就像魅力一样) 如果我拔下网线或关掉adsl,上传挂起和超时都无法正常工作。我做了几次测试,但没有任何效果,这会让我发疯 这是我的代码:

public boolean uploadFile2(String localFile, String serverFile) {
    try {
        InputStream stO = new FileInputStream(localFile);
        OutputStream stD = storeFileStream(serverFile);
        setDataTimeout(dataTimeOut);
        Util.copyStream(stO, stD, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, new CopyStreamAdapter() {

            public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
                System.out.println("On transfert " + "/" + totalBytesTransferred + "/" + bytesTransferred + "/" + streamSize);
            }
        });
        completePendingCommand();
    } catch (Exception e) {
    }
    return false;
}

public boolean connectAndLogin(String host, String userName, String password) throws IOException, UnknownHostException,
        FTPConnectionClosedException {
    boolean success = false;
    setDefaultTimeout(this.defaultTimeOut);
    setConnectTimeout(this.connectionTimeOut);
    addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    setPassiveMode(true);
    connect(host);
    int reply = getReplyCode();
    if (FTPReply.isPositiveCompletion(reply)) {
        success = login(userName, password);
        setKeepAlive(true);
        setControlKeepAliveTimeout(keepAliveTimeout);
        setControlKeepAliveReplyTimeout(keepAliveResponseTimeout);
        setSoTimeout(soTimeOut);
        setDataTimeout(dataTimeOut);
        setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
        setFileType(FTP.BINARY_FILE_TYPE);
    }
    if (!success) {
        disconnect();
    }
    return success;
}

感谢任何帮助,谢谢

这也不起作用:

FileInputStream in = new FileInputStream(localFile);
boolean result = storeFile(serverFile, in);
in.close();
return result;

0 个答案:

没有答案