【问题标题】:Download files from Android FTPServer从 Android FTPServer 下载文件
【发布时间】:2015-12-28 13:56:24
【问题描述】:

我写了一个简单的 java 应用程序,它会使用 Apache Commons Net 库从 FTP 服务器下载文件,服务器位于安卓手机上。我尝试了几个设置和几个 FTPServer 应用程序,但我无法让它工作。该应用程序只是在开始下载后挂起。到目前为止,我发现的唯一解决方法是设置数据超时,然后捕获异常并创建一个具有新连接的新客户端实例以下载下一个文件。有谁知道怎么弄好?

    FileOutputStream outputStream = new FileOutputStream(local);

    boolean downloaded = false;
    try {
        //InputStream retrieveFileStream = getFtpClient().retrieveFileStream(remoteFilePath);
        //new BufferedOutputStream(outputStream);
        FTPClient c = new FTPClient();
        c= new FTPClient();
        c.connect(host, port);
        c.enterLocalPassiveMode();
        c.setSoTimeout(1000 * 20);
        c.setDefaultTimeout(1000 * 20);
        c.login(user, pass);
        c.setBufferSize(1024 * 8);
        c.setFileType(FTP.BINARY_FILE_TYPE);
        c.setDataTimeout(20 * 1000);
        downloaded = c.retrieveFile(remoteFilePath, outputStream);

        if (!downloaded) {
            String[] replyStrings = c.getReplyStrings();
            LOG.info("not downloaded code {} Messages {}", c.getReplyCode(), Arrays.toString(replyStrings));
            local.delete();
        } else {
            LOG.info("file was downloaded");
        }
    } catch (IOException iOException) {
        if (!(iOException.getCause() instanceof SocketTimeoutException)) {
            LOG.info("Caught iOException do shut down"  + iOException.getMessage());
            logoutAndDisconnect();
        } else {
            LOG.info("Caught SocketTimeoutException assuming file was downloaded " + iOException.getMessage());
            downloaded = true;
        }
    } catch (Exception e) {
        LOG.error("ERROR: ", e.getMessage(), e);
        logoutAndDisconnect();
    } finally {
        outputStream.close();
    }
    return downloaded;
}

这里描述了一个类似的问题http://www.egeek.me/2012/11/22/downloading-a-file-via-ftp-in-android/,但我是本地网络

【问题讨论】:

    标签: java ftp


    【解决方案1】:

    是手机上的 DroidWall 阻止了连接。禁用它或允许内核连接通常可以解决问题。

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 1970-01-01
      • 2012-12-25
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2017-11-22
      相关资源
      最近更新 更多