简体   繁体   English

如何将字节数组转换为文件

[英]How to convert byte array to file

I have connected to an ftp location using; 我已使用连接到ftp位置 ;

URL url = new URL("ftp://user:password@mydomain.com/" + file_name +";type=i");

I read the content into a byte array as shown below; 我将内容读入一个字节数组,如下所示;

byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) > 0) 
{
    //check if bytes in buffer is a file
}

I want to be able to check if the bytes in buffer is a file without explicitly passing a specific file to write to it like; 我希望能够检查buffer的字节是否是一个文件而没有显式传递特定文件来写入它;

    File xfile= new File("dir1/");
    FileOutputStream fos = new FileOutputStream(xfile);
    fos.write(bytes);

    if(xfile.isFile())
    {

    }

In an Ideal world something like this; 在这样的理想世界中;

    File xfile = new File(buffer);//Note: you cannot do this in java
    if(xfile.isFile())
    {

    }

isFile() is to check if the bytes read from the ftp is file. isFile()用于检查从ftp读取的字节是否为文件。 I don't want to pass an explicit file name as I do not know the name of the file on the ftp location . 我不想传递显式文件名,因为我不知道ftp位置上文件的名称。

Any solutions available? 任何解决方案?

What is a file ? 什么是文件

A computer file is a block of arbitrary information [...] which is available to a computer program and is usually based on some kind of durable storage. 计算机文件是计算机程序可用的任意信息块,通常基于某种持久存储。 A file is durable in the sense that it remains available for programs to use after the current program has finished. 文件是持久的,因为它在当前程序完成后仍可供程序使用。

Your bytes that are stored in the byte array will be a part of a file if you write them on some kind of durable storage . 存储在字节数组中的字节将是文件的一部分,如果您在某种类型的持久存储上写入它们

Sure, we often say that we read a file or write a file, but basically we read bytes from a file and write bytes to a file . 当然,我们经常说我们读取文件或文件,但基本上我们从文件中读取字节并将字节写入文件

So we can't test a byte array whether it's content is a file or not. 所以我们无法测试字节数组是否内容是文件。 Simply because every byte array can be used to create a file (even an empty array). 只是因为每个字节数组都可以用来创建一个文件(甚至是一个空数组)。

BTW - the ftp server does not send a file, it (1) reads bytes and (2) a filename and (3) sends the bytes and (4) the filename so that a client can (5) read the bytes and (6) the filename and use both datasets to (7) create a file. BTW - ftp服务器不发送文件,它(1)读取字节和(2)文件名和(3)发送字节和(4)文件名,以便客户端可以(5)读取字节和(6) )文件名并使用两个数据集来(7)创建一个文件。 The ftp server doesn't have to access a file, it can take bytes and names from a database or create both in memory... 该FTP服务器不具备访问一个文件,它可以采取字节和名字从数据库或内存中创建两个...

I guess you cannot check if the byte[] array is a file or not. 我猜你不能检查byte []数组是否是一个文件。 Why dont' you just use already written and tested library like maybe for example: http://commons.apache.org/net/ 为什么不'你只是使用已经编写和测试的库,例如: http//commons.apache.org/net/

There is no way to do that easily. 没有办法轻易做到这一点。

A file is a byte array on a disk and a byte array will be a file if you write it to disk. 文件是磁盘上的字节数组,如果将文件写入磁盘,则字节数组将是文件。 There is no reliable way of telling what is in the data you just received, without parsing the data and checking if you can find a valid file header in it. 有告诉什么是你刚接收的数据,而不解析数据,并检查,如果你能在那里找到一个有效的文件头没有可靠的方法。

Where is isFile() file means the content fetched from from the ftp stream is a file. isFile()文件在哪里表示从ftp流中获取的内容是一个文件。

The answer to that is simple. 答案很简单。 You can't do it because it doesn't make any sense. 你做不到,因为它没有任何意义。

  • What you have read from the stream IS a sequence of bytes stored in memory. 您从流中读取的内容是存储在内存中的一系列字节。

  • A file is a sequence of bytes stored on a disk (typically). 文件是存储在磁盘上的字节序列(通常)。

These are not the same thing. 这些都不是一回事。 (Or if you want to get all theoretical / philosophical you have to answer the question "when is a sequence of bytes a file, and when is it not a file". (或者如果你想获得所有的理论/哲学,你必须回答这个问题“什么时候是文件的字节序列,什么时候不是文件”。


Now a more sensible question to ask might be: 现在一个更明智的问题可能是:

How do I know if the stuff I fetched by FTP is the contents of a file on the FTP server. 我怎么知道我通过FTP获取的东西是否是FTP服务器上文件的内容。

(... as distinct from a rendering of a directory or something). (...与目录或其他东西的渲染不同)。

The answer is that you can't be sure if you fetched the file by opening an URLConnection to the FTP server ... like you have done. 答案是你无法确定是否通过打开FTP服务器的URLConnection来获取文件......就像你已经完成的那样。 It is like asking "is '(123) 555-5555' a phone number?". 这就像问“是'(123)555-5555'一个电话号码?”。 It could be a phone number, or it could just be a sequence of characters that look like a phone number. 可以是电话号码,也可以是一系列看起来像电话号码的字符。

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

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