0

I receive an UTF-8 encoded JSON String va REST web service and have to create a file from the data. The string is encoded in C# like:

UTF8Encoding encoding = new UTF8Encoding();
//fill object data
byte[] bytes = encoding.GetBytes(object.Serialize());

then I deserialize the JSON object and decode in java the data I need like:

requestData is the JSON string

requestData = requestData.substring(requestData.indexOf("{")+1, requestData.lastIndexOf("}") - requestData.indexOf("{") -1).replace("\"","");
        for(String s : requestData.split(",")) {
            String[] pair = s.split(":");
            if(pair[0].equals("data")) {
                binaryData = pair[1].getBytes("UTF-8");
            }
        }
return binaryData;

binaryData should be a byte array representing a .docx file. But when I write the byte array into a fileand download this, the downloaded file can be opend but the text of the file is just the byte array like that.

UEsDBBQABgAIAAAAIQCuDuWM4wEAANMIAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC0ls1u2zAQhO8F8g4Cr4FEJ4egKCzn0KbHJkBdtFeaXFlERVIg17H99l1KtuAkqqVW8cWATM3Mx+XPan6/M1XyDD5oZ3N2k81YAlY6pe06Zz+WX9OPLAkorBKVs5CzPQR2v7j6MF/uawgJqW3IWYlYf+I8yBKMCJmrwdJI4bwRSI9+zWshf4s18NvZ7I5LZxEsphg92GL

any ideas what I'm doing wrong, best reguards.

EDIT: I write the byte[] into the file with

FileUtils.writeByteArrayToFile(dataFile, file);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
bersch
  • 3
  • 2
  • What is "object.Serialize()"? Also, would be useful if you pasted the content create by c# app. – Jakub Binkowski Jan 09 '18 at 15:28
  • `object.Serialize()` creates the json serialized string as I see (not my code) and in the requst I get contet like `"{"id":"0","name":"94aff792-e1ad-48f3-ac35-09ecbaf88edb_test","data":"UEsDBBQABgAIAAAAIQCuDuWM4wEAANMIAAATAAgCW0NvbnRlbn...` and I want to create from the data a word file @JakubBinkowski – bersch Jan 09 '18 at 15:49
  • Your data is base64 encoded. You need to call `Convert.FromBase64String` to get the encoded byte array - no need to additionally use the UTF8 encoder. – ckuri Jan 09 '18 at 16:47
  • @ckuri `Convert.FromBase64String(erledigungsWord.Serialize());` here I get a `System.FormatExeption` because `erledigungsWord.Serialize()` is not base64 encoded – bersch Jan 09 '18 at 17:37
  • I’m talking about your`binaryData` Variable and it’s JSON serialisation with the JSON key `data`. If I input its value of `UEsDB…` into a Base64 decoder I get an DOCX file (or at least the beginning of it). – ckuri Jan 10 '18 at 01:09
  • @ckuri yea thank you thats it! – bersch Jan 10 '18 at 08:51

1 Answers1

0

At:

binaryData = pair[1].getBytes("UTF-8");

pair[1] appears to be Base64 encoded, so it should be decoded at the Java side. There are a few ways to decode Base64 in Java. You can try replacing this with:

binaryData = Base64.getDecoder().decode(pair[1]);
fgb
  • 18,439
  • 2
  • 38
  • 52