如何从一个文件的字节数组的文件名? [英] How to get a file name from a file's byte array?
问题描述
我有个字节重新presenting,我发送在网络上的文件。除了文件系统上手动重建文件,哪能从文件获取信息,如的getName()的getPath()等?
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?
在换句话说:
- 我开始了与机器A文件
- 我使用的文件实用程序打开文件转换成字节数组
- 我在网络上的文件传输到计算机B
- 在计算机B上,我想该字节[]重建成一个文件并运行的方法,如的getName()
以下不工作
- (文件)字节 - >不转换
- ((文件)((对象)字节))) - >不转换为
我真的宁可不上创建filesytem一个新的临时文件,虽然我知道有可用的静态File.createTemp会做到这一点。我想preFER只保留在内存中,从字节构造一个新File对象[]数组,得到我需要的信息并完成。
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.
事实上,这将是更好的是,将通过解析位采取的byte [],并从它,直接将文件名的API。
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.
推荐答案
的字节[]
由返回 FileUtils.readFileToByteArray
仅仅是文件内容,仅此而已。
The byte[]
that is returned by FileUtils.readFileToByteArray
is only the file contents, nothing else.
您应该创建自己的类,实现了Serializable接口,其中包括两个字段:字节[]
的文件内容,以及 java.io .File
有一切你需要的。然后,序列化/反序列化类到字节[]
,被发送。
You should create your own class that is Serializable that includes two fields: a byte[]
for the file contents, and a java.io.File
that has everything else you need. You then serialize/deserialize your class into byte[]
, which is transmitted.
这篇关于如何从一个文件的字节数组的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!