我已经连接到一个 ftp 位置,使用;
URL url = new URL("ftp://user:password@mydomain.com/" + file_name +";type=i");
我把内容读入一个字节数组,如下所示;
byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) > 0)
{
//check if bytes in buffer is a file
}
我希望能够检查 buffer
中的字节是否是一个文件,而不需要显式地传递一个特定的文件来写入它;
File xfile= new File("dir1/");
FileOutputStream fos = new FileOutputStream(xfile);
fos.write(bytes);
if(xfile.isFile())
{
}
在理想世界中是这样的;
File xfile = new File(buffer);//Note: you cannot do this in java
if(xfile.isFile())
{
}
isFile()
是检查从ftp读取的字节是否是文件。我不想传递明确的文件名,因为我不知道ftp 位置 上的文件名。
有什么解决方案吗?
最佳答案
什么是文件?
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.
如果您将它们写入某种持久存储,则存储在字节数组中的字节将成为文件的一部分。
当然,我们经常说我们读一个文件或写一个文件,但基本上我们从文件中读取字节和将字节写入文件。
所以我们不能测试一个字节数组是否它的内容是一个文件。仅仅是因为每个字节数组都可以用来创建一个文件(即使是一个空数组)。
BTW - ftp 服务器不发送文件,它 (1) 读取字节和 (2) 文件名 (3) 发送字节和 (4) 文件名,以便客户端可以 (5) 读取字节(6) 文件名并使用两个数据集来 (7) 创建文件。 ftp 服务器没有访问文件,它可以从数据库中获取字节和名称,或者在内存中创建两者...
关于java - 如何将字节数组转换为文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7174449/