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;
我希望能够检查缓冲区中的字节是否是一个文件而没有显式传递特定文件来写入它;
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. I don't want to pass an explicit file name as I do not know the name of the file on the ftp location.
isFile()用于检查从ftp读取的字节是否为文件。我不想传递显式文件名,因为我不知道ftp位置上文件的名称。
Any solutions available?
任何解决方案?
4 个解决方案
#1
2
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. The ftp server doesn't have to access a file, it can take bytes and names from a database or create both in memory...
BTW - ftp服务器不发送文件,它(1)读取字节和(2)文件名和(3)发送字节和(4)文件名,以便客户端可以(5)读取字节和(6) )文件名并使用两个数据集来(7)创建一个文件。 ftp服务器不必访问文件,它可以从数据库中获取字节和名称,或者在内存中创建...
#2
1
I guess you cannot check if the byte[] array is a file or not. Why dont' you just use already written and tested library like maybe for example: http://commons.apache.org/net/
我猜你不能检查byte []数组是否是一个文件。为什么不'你只是使用已经编写和测试的库,例如:http://commons.apache.org/net/
#3
1
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.
文件是磁盘上的字节数组,如果将文件写入磁盘,则字节数组将是文件。没有可靠的方法来告诉您刚收到的数据中的内容,而无需解析数据并检查是否可以在其中找到有效的文件头。
#4
0
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. It is like asking "is '(123) 555-5555' a phone number?". It could be a phone number, or it could just be a sequence of characters that look like a phone number.
答案是你无法确定是否通过打开FTP服务器的URLConnection来获取文件......就像你已经完成的那样。这就像问“是'(123)555-5555'一个电话号码?”。它可以是电话号码,也可以是一系列看起来像电话号码的字符。
#1
2
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. The ftp server doesn't have to access a file, it can take bytes and names from a database or create both in memory...
BTW - ftp服务器不发送文件,它(1)读取字节和(2)文件名和(3)发送字节和(4)文件名,以便客户端可以(5)读取字节和(6) )文件名并使用两个数据集来(7)创建一个文件。 ftp服务器不必访问文件,它可以从数据库中获取字节和名称,或者在内存中创建...
#2
1
I guess you cannot check if the byte[] array is a file or not. Why dont' you just use already written and tested library like maybe for example: http://commons.apache.org/net/
我猜你不能检查byte []数组是否是一个文件。为什么不'你只是使用已经编写和测试的库,例如:http://commons.apache.org/net/
#3
1
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.
文件是磁盘上的字节数组,如果将文件写入磁盘,则字节数组将是文件。没有可靠的方法来告诉您刚收到的数据中的内容,而无需解析数据并检查是否可以在其中找到有效的文件头。
#4
0
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. It is like asking "is '(123) 555-5555' a phone number?". It could be a phone number, or it could just be a sequence of characters that look like a phone number.
答案是你无法确定是否通过打开FTP服务器的URLConnection来获取文件......就像你已经完成的那样。这就像问“是'(123)555-5555'一个电话号码?”。它可以是电话号码,也可以是一系列看起来像电话号码的字符。