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?
In other words:
The following does not work
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.
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.
The byte[]
that is returned by FileUtils.readFileToByteArray
is only the file contents, nothing else.
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.
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.
If you want the name you need to transfer it separately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With