11

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:

  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()

The following does not work

  1. (File) bytes --> Does not convert
  2. ((File) ((Object) bytes))) --> Does not convert either

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.

6
  • Does FileUtils output data about the file and/or the file's contents? Commented Nov 11, 2013 at 17:31
  • FileUtils will dump the byte[] into an actual File, on the File System itself. But, I just want to access the File from memory. I.e., just grab the .getName() contents. See FileUtils.writeByteArrayToFile(file, data); Commented Nov 11, 2013 at 17:37
  • You have to send the name/path/etc. across the network along with the bytes. Commented Nov 11, 2013 at 17:37
  • @Kevin isn't the file name information stored in the byte[] though? Commented Nov 11, 2013 at 17:38
  • No. The byte[] has the contents of the file, no more, no less. Well, maybe less if there's a problem reading. It's no different from any other byte[]. Commented Nov 11, 2013 at 17:40

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

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.

Comments

0

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

file Name then separator then file Path then separator then file bytes

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.