【问题标题】:Read first bytes of a file读取文件的第一个字节
【发布时间】:2013-04-14 14:10:03
【问题描述】:

我需要一个非常简单的函数,允许我通过 FTP 读取文件的前 1k 字节。我想在 MATLAB 中使用它来读取第一行,并根据一些参数,最终只下载我真正需要的文件。我在网上找到了一些不幸的是不起作用的例子。在这里,我提出了我试图下载一个文件的示例代码(我正在使用 Apache 库)。

FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {
        client.connect("data.site.org");

        // filename to be downloaded. 
        String filename = "filename.Z";
        fos = new FileOutputStream(filename);

        // Download file from FTP server
        InputStream stream = client.retrieveFileStream("/pub/obs/2008/021/ab120210.08d.Z");
        byte[] b = new byte[1024];
        stream.read(b);
        fos.write(b);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

错误在返回空的流中。我知道我以错误的方式传递文件夹名称,但我不明白我该怎么做。我尝试了很多方法。

我也尝试过使用 URL 的 Java 类:

    URL url;

    url = new URL("ftp://data.site.org/pub/obs/2008/021/ab120210.08d.Z");

    URLConnection con = url.openConnection();
    BufferedInputStream in =
            new BufferedInputStream(con.getInputStream());
    FileOutputStream out =
            new FileOutputStream("C:\\filename.Z");

    int i;
    byte[] bytesIn = new byte[1024];
    if ((i = in.read(bytesIn)) >= 0) {
        out.write(bytesIn);
    }
    out.close();
    in.close();

但是当我关闭 InputStream 时它会出错!

我肯定被卡住了。一些 cmets 会非常有用!

【问题讨论】:

  • 欢迎来到 Stackoverflow!无需为您的标题添加标签,有一个标签系统。请阅读meta.stackexchange.com/q/19190/147072 了解更多信息。此外,您无需在末尾添加“谢谢”或您的名字,每个人都感谢您的帮助,并且您的名字显示在您创建的每个问题和答案的右下角的字符表中。

标签: java matlab ftp


【解决方案1】:

试试这个测试

    InputStream is = new URL("ftp://test:test@ftp.secureftp-test.com/bookstore.xml").openStream();
    byte[] a = new byte[1000];
    int n = is.read(a);
    is.close();
    System.out.println(new String(a, 0, n));

确实有效

【讨论】:

  • 您好,该代码仅在下载整个文件时才有效。我添加了两行,将数据存储在本地驱动器的输出文件中。当我从 MATLAB 运行代码时,它会下载文件(我可以在文件夹中看到它并且可以打开它),但它卡在输入连接关闭的行上(is.close())。如果我删除该行,它会起作用。问题是我不知道如何管理连接,我想避免无缘无故地保持开放端口。最后一件事。如果我下载所有文件, is.close() 工作正常!这让我抓狂:)。
【解决方案2】:

根据我的经验,当您从ftpClient.retrieveFileStream 获取的流中读取字节时,对于第一次运行,不能保证您的字节缓冲区已填满。但是,您应该读取 stream.read(b); 的返回值并用基于它的循环包围,或者使用高级库来填充 1024 长度的 byte[] 缓冲区:

InputStream stream = null;
try {
    // Download file from FTP server
    stream = client.retrieveFileStream("/pub/obs/2008/021/ab120210.08d.Z");
    byte[] b = new byte[1024];
    IOUtils.read(stream, b); // will call periodically stream.read() until it fills up your buffer or reaches end-of-file
    fos.write(b);

} catch (IOException e) {
    e.printStackTrace();
} finally {
    IOUtils.closeQuietly(inputStream);
}

【讨论】:

    【解决方案3】:

    我不明白为什么它不起作用。我发现了这个link,他们每次使用 Apache 库读取 4096 个字节。我读取了前 1024 个字节,它最终可以工作,唯一的问题是,如果使用了 completePendingCommand(),则程序将永远保留。因此,我已将其删除,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多