简体   繁体   English

读取文件的第一个字节

[英]Read first bytes of a file

I need a very simple function that allows me to read the first 1k bytes of a file through FTP. 我需要一个非常简单的函数,允许我通过FTP读取文件的前1k字节。 I want to use it in MATLAB to read the first lines and, according to some parameters, to download only files I really need eventually. 我想在MATLAB中使用它来读取第一行,并根据一些参数,下载最终我真正需要的文件。 I found some examples online that unfortunately do not work. 我在网上发现了一些不幸的例子。 Here I'm proposing the sample code where I'm trying to download one single file (I'm using the Apache libraries). 在这里,我提出了示例代码,我正在尝试下载一个单独的文件(我正在使用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();
        }
    }

the error is in stream which is returned empty. 错误在流中返回为空。 I know I'm passing the folder name in a wrong way, but I cannot understand how I have to do. 我知道我以错误的方式传递文件夹名称,但我无法理解我该怎么做。 I've tried in many way. 我试过很多方面。

I've also tried with the URL's Java classes as: 我也尝试过使用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();

but it is giving an error when I'm closing the InputStream in! 但是当我关闭InputStream时它会出错!

I'm definitely stuck. 我肯定卡住了。 Some comments about would be very useful! 一些评论会非常有用!

Try this test 试试这个测试

    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));

it definitely works 它确实有效

From my experience when you read bytes from a stream acquired from ftpClient.retrieveFileStream , for the first run it is not guarantied that you get your byte buffer filled up. 根据我从ftpClient.retrieveFileStream获取的流中读取字节的经验,对于第一次运行,不保证您填充了字节缓冲区。 However, either you should read the return value of stream.read(b); 但是,您应该读取stream.read(b);的返回值stream.read(b); surrounded with a cycle based on it or use an advanced library to fill up the 1024 length byte[] buffer: 用基于它的循环包围或使用高级库来填充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);
}

I cannot understand why it doesn't work. 我无法理解为什么它不起作用。 I found this link where they used the Apache library to read 4096 bytes each time. 我找到了这个链接 ,他们使用Apache库每次读取4096个字节。 I read the first 1024 bytes and it works eventually, the only thing is that if completePendingCommand() is used, the program is held for ever. 我读了前1024个字节并且它最终起作用,唯一的事情是如果使用了completePendingCommand(),程序将永远保留。 Thus I've removed it and everything works fine. 因此我删除了它,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM