0

So I read a file into a byte array and then I break it up into chunks and send it across the network with UDP.

Path path = Paths.get("files_upload/music.mp3");
byte[] objectBytes = Files.readAllBytes(path);

On the server I read all the chunks into a buffer and I end up with the same byte[] objectBytes as I had on the client. Now I want to write the file to disk using the original file name which is music.mp3 in this case. So how can I get the file name from the array of bytes?

csss
  • 1,937
  • 2
  • 14
  • 24

1 Answers1

3

The array of bytes doesn't contain the file name. You'd have to send it separately. You can call getFileName on your path, and then turn that into a byte array using getBytes() on the resulting string.

String fileName = path.getFileName();
byte[] fileNameBytes = fileName.getBytes();

You can then send this first and read it on the other end. Note, this wont contain the whole path, only the name of the file (music.mp3 in your case).

By the way, are you sure you want to be using UDP? What if you lose a packet or two when the data is being transferred? How do you detect that on the server?

Neal
  • 6,722
  • 4
  • 38
  • 31