writing byte[] to file giving corrupt file
<p>I am getting byte[] in request parameter in servlet which I am fetching in string and then again converting it into byte[] :</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>String encodingScheme = "UTF-8";
request.setCharacterEncoding(encodingScheme);
String requestStr = request.getParameter("inputstream");
byte[] rawRequestMsg = requestStr.getBytes(encodingScheme);</code></pre>
</div>
</div>
</p>

<p>Now this byte[] I am trying to write to a .docx file as this byte[] which I am using is byte[] representation of a docx file only. Code for writing this to file is like :</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>String uploadedFileLocation = fileLocation;
FileOutputStream fileOuputStream = new FileOutputStream("path till .docx file");
fileOuputStream.write(byteArray);
fileOuputStream.close();</code></pre>
</div>
</div>
</p>

<p>The problem is the .docx file being created is corrupt and unable to open, but when I change it to .doc then I can open it but instead of seeing the text content I see only the byte[] sequence there like below :</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 33, 0, -84, -122, 80, 87, -114, 1, 0, 0, -64, 5, 0, 0, 19, 0, 8, 2, 91, 67, 111, 110, 116, 101, 110, 116, 95, 84, 121, 112, 101, 115, 93, 46, 120, 109, 108, 32, -94, 4, 2, 40, -96, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, </code></pre>
</div>
</div>
</p>

<p>Dont know how to write that correctly.
Need help.
Thanks,
Samir</p>

<p>Actually the code below used to work which is of a REST webservice  </p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>@
POST@ Path("/binaryfileupload/{filename}")@ Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response upload(byte[] input, @PathParam("filename") String filename) {
  FileOutputStream fileOuputStream = new FileOutputStream(uploadedFileLocation);
  fileOuputStream.write(input);
  fileOuputStream.close();
}</code></pre>
</div>
</div>
</p>

<p>Only change I made is from here this input which is byte[] I am sending to servlet and in servlet want to write file instead of writing in my webservice(which was working correctly).</p>