commons.net如果与服务器的连接丢失,FTPSClient.storeFile不会抛出IOException

时间:2013-06-18 21:29:16

标签: timeout

背景:

我正在尝试为使用Apache Commons.net FTPSClient传输文件的应用程序添加一些级别容错。如果客户端和服务器之间的连接失败,我想捕获生成的异常/返回代码,记录详细信息,并尝试重新连接/重试传输。

什么有效:

retrieveFile()方法。如果连接失败,(即我禁用了服务器的公共接口),在我指定为超时的时间之后,我收到一个由SocketTimeoutException引起的CopyStreamException。

什么行不通:

storeFile()方法。如果我通过storeFile()启动传输并禁用服务器的公共接口,则storeFile()方法会无限期地阻塞/挂起而不会抛出任何异常。

如果连接终止,这是一个挂起的简单应用程序:

public class SmallTest {

private static Logger log = Logger.getLogger(SmallTest.class);

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

    FTPSClient client = new FTPSClient(true);
    FTPSCredentials creds = new FTPSCredentials("host", "usr", "pass", 
            "/keystore/ftpclient.jks", "pass", 
            "/keystore/rootca.jks");

    String file = "/file/jdk-7u21-linux-x64.rpm";
    String destinationFile = "/jdk-7u21-linux-x64.rpm";

    client.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
    client.setKeyManager(creds.getKeystoreManager());

    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.setCopyStreamListener(createListener());

    client.setConnectTimeout(5000);
    client.setDefaultTimeout(5000);

    client.connect(creds.getHost(), 990);   

    client.setSoTimeout(5000);
    client.setDataTimeout(5000);

    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        client.disconnect();
        log.error("ERROR: " + creds.getHost() + " refused the connection");
    } else {
        if (client.login(creds.getUser(), creds.getPass())) {
            log.debug("Logged in as " + creds.getUser());

            client.enterLocalPassiveMode();     
            client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
            client.setFileType(FTP.BINARY_FILE_TYPE);

            InputStream inputStream = new FileInputStream(file);

            log.debug("Invoking storeFile()");

            if (!client.storeFile(destinationFile, inputStream)) {
                log.error("ERROR: Failed to store " + file
                        + " on remote host. Last reply code: "
                        + client.getReplyCode());
            } else {
                log.debug("Stored the file...");
            }

            inputStream.close();
            client.logout();
            client.disconnect();
        } else {
            log.error("Could not log into " + creds.getHost());
        }
    }
}

private static CopyStreamListener createListener(){
    return new CopyStreamListener(){
        private long megsTotal = 0;
        @Override
        public void bytesTransferred(CopyStreamEvent event) {
            bytesTransferred(event.getTotalBytesTransferred(), event.getBytesTransferred(), event.getStreamSize());
        }
        @Override
        public void bytesTransferred(long totalBytesTransferred,
                int bytesTransferred, long streamSize) {
            long megs = totalBytesTransferred / 1000000;
            for (long l = megsTotal; l < megs; l++) {
                System.out.print("#");
            }
            megsTotal = megs;
        }
    };
}

有没有办法让连接ACTUALLY超时​​?

SW版本:

Commons.net v3.3 Java 7 CentOS 6.3

提前致谢,

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我认为当我拔掉笔记本电脑上的以太网线时,我能够得到一些似乎与所需超时行为一致的东西。

我使用'storeFileStream'而不是'storeFile',然后使用'completePendingCommand'来完成传输。您可以查看Apache commons文档中的'completePendingCommand'以查看此类传输的示例。我花了大约15分钟才能暂停。另一件事:上述文档包括调用'isPositiveIntermediate'来检查错误,但这不起作用。我用'isPositivePreliminary'替换它,现在它似乎工作。我不确定这是否真的是正确的,但这是迄今为止我发现的最好的。