Java 如何从文件的字节数组中获取文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19912267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:15:15  来源:igfitidea点击:

How to get a file name from a file's byte array?

javaarraysapachefileapache-commons

提问by E.S.

I have the bytes representing a file that I am transmitting over a network. Besides reconstructing the file manually on the file system, how can I get the information from the File, such as getName(), getPath(), etc?

我有代表我通过网络传输的文件的字节。除了在文件系统上手动重建文件外,如何从文件中获取信息,例如 getName()、getPath() 等?

In other words:

换句话说:

  1. I start out with a File on machine A
  2. I use FileUtils to turn the file into a byte array
  3. I transmit that file over a network to machine B
  4. On machine B, I want to reconstruct that byte[] into a File and run methods such as getName()
  1. 我从机器 A 上的文件开始
  2. 我使用 FileUtils 将文件转换为字节数组
  3. 我通过网络将该文件传输到机器 B
  4. 在机器 B 上,我想将该 byte[] 重构为一个 File 并运行诸如 getName() 之类的方法

The following does not work

以下不起作用

  1. (File) bytes --> Does not convert
  2. ((File) ((Object) bytes))) --> Does not convert either
  1. (文件)字节 --> 不转换
  2. ((File) ((Object) bytes))) --> 也不转换

I would really rather not create a new temporary file on the filesytem, although I know there are static File.createTemp available that will do that. I'd prefer to just keep it in memory, construct a new File object from the byte[] array, get the information I Need and be done.

我真的不想在文件系统上创建一个新的临时文件,尽管我知道有静态 File.createTemp 可以做到这一点。我更愿意将它保存在内存中,从 byte[] 数组构造一个新的 File 对象,获取我需要的信息并完成。

Actually, what would be even better is an API that will take the byte[] and from it, directly get the file name by parsing the bits.

实际上,更好的是一个 API,它将获取 byte[] 并从中通过解析位直接获取文件名。

采纳答案by Glenn Lane

The byte[]that is returned by FileUtils.readFileToByteArrayis only the file contents, nothing else.

byte[]由返回FileUtils.readFileToByteArray仅仅是文件内容,没有别的。

You should create your own class that is Serializable that includes two fields: a byte[]for the file contents, and a java.io.Filethat has everything else you need. You then serialize/deserialize your class into byte[], which is transmitted.

您应该创建自己的 Serializable 类,该类包括两个字段:一个byte[]用于文件内容,一个java.io.File包含您需要的所有其他内容。然后将您的类序列化/反序列化为byte[],然后进行传输。

回答by C. Ramseyer

The file contents and its name are two separate and independent things. While a specific file format could have metadata to store the name in the contents (similar to e.g. ID3 tags for MP3), in a typical file there is no way to know the name from byte [] contents. Also even if, it would be the name from a remote machine which may be invalid on the target platform.

文件内容及其名称是两个独立且独立的事物。虽然特定的文件格式可以有元数据来存储内容中的名称(类似于 MP3 的 ID3 标签),但在典型文件中,无法从byte [] contents. 此外,即使它是来自远程机器的名称,在目标平台上可能无效。

If you want the name you need to transfer it separately.

如果您想要名称,则需要单独传输。

回答by ChuckCottrill

As others have correctly stated, FileUtils is giving you the contents of the file.

正如其他人正确指出的那样, FileUtils 正在为您提供文件的内容。

You want to transfer file meta-data (filename/pathname, create/modify/access time, ownership, permissions, size, and (probably) a checksum) in addition to the contents of the file. One way you could do this is to place the file into a container (tar, shar, zip, etc), which provides the file meta-data as well as the file contents. Or you could use a file transfer protocol which transfers this meta-data.

除了文件内容之外,您还想传输文件元数据(文件名/路径名、创建/修改/访问时间、所有权、权限、大小和(可能)校验和)。一种方法是将文件放入容器(tar、shar、zip 等)中,该容器提供文件元数据和文件内容。或者您可以使用传输此元数据的文件传输协议。

回答by Error

you can as well invent your own protocol for sending information with file Bytes Mine as follow

您也可以发明自己的协议来使用文件 Bytes Mine 发送信息,如下所示

file Namethen separator then file Paththen separator then file bytes

file Name然后分离器file Path然后分离器然后file bytes

separatorshould be character that can't be used as fileName for file Path (i.e |, :, & ...)

separator应该是不能用作文件路径的文件名的字符(即 |, :, & ...)

deassembleat receiving client deassemble bytes in a reverse order and extract information sent.

deassemble在接收客户端以相反的顺序分解字节并提取发送的信息。