3

I was trying to generate PDf from byte array in Java which was returned through webservice, But the PDF cannot be opened. It shows its corrupted, I have attached my code. Anyone pls help about where i got wrong?

 JSONObject o = new JSONObject(outjson); JSONObject jsonob = o.optJSONObject("PDF details"); byte[] pdfbyte=jsonob.optString("pdf bytearray").toString().getBytes(); String str1 = new String(pdfbyte); File someFile = new File("C:/Users/acer/Desktop/test1.pdf"); FileOutputStream fos = new FileOutputStream(someFile); byte[] byteData = str1.getBytes(); byte[] byteData1 = test.getBytes(); fos.write(pdfbyte); fos.flush(); fos.close(); 

Following is my JSON from webservice:

{"PDF details": { "id":"121", "pdf bytearray":"[B@62a58cd" } } 

Following is my webservice code which outputs bytearray in json:

public Response getPdf( ) { String flag=null; File file = new File("C:/Users/acer/Desktop/Report.pdf"); FileInputStream fileInputStream; byte[] data = null; byte[] finalData = null; ByteArrayOutputStream byteArrayOutputStream = null; fileInputStream = new FileInputStream(file); data = new byte[(int)file.length()]; finalData = new byte[(int)file.length()]; byteArrayOutputStream = new ByteArrayOutputStream(); fileInputStream.read(data); byteArrayOutputStream.write(data); finalData = byteArrayOutputStream.toByteArray(); fileInputStream.close(); System.out.println(finalData); JSONObject jsonObject = new JSONObject(); JSONObject mainjsonObject = new JSONObject(); jsonObject.put("id","121"); jsonObject.put("pdf bytearray",finalData); mainjsonObject.put("PDF details",jsonObject); flag = "" + mainjsonObject; return Response.status(200).entity(flag).build(); } 
4
  • The pdf bytearray from your webservice is not valid. You need to fix the webservice. Commented Jul 26, 2017 at 6:09
  • i will attach my webservice code..pls check whethr the prblm is with it? Commented Jul 26, 2017 at 6:11
  • 1
    byte[].toString() doesn't generate a string containing the content of the byte array. Just a string containing the type of the array ([B), followed by @ followed by a hashCode (62a58cd). You should be able to realize by yourself that a whole PDF document can't possibly fit into 10 characters. Use base-64 to transform the pdf bytes into a printable string, and vice-versa. Commented Jul 26, 2017 at 6:14
  • 3
    I already said it. Take a few minutes to read and digest my comment, think, and do some research. And while you're at it, avoid using spaces and random uppercase characters in the keys of your JSON objects. Use Java variable naming conventions: pdfDetails, etc. Commented Jul 26, 2017 at 6:16

1 Answer 1

2

i got it right with following chnge in my webservice:

public Response getPdf( ) { String flag=null; File file = new File("C:/Users/acer/Desktop/Report.pdf"); FileInputStream fileInputStreamReader = new FileInputStream(file); byte[] bytes = new byte[(int)file.length()]; fileInputStreamReader.read(bytes); String encodedBase64 = new String(Base64.encodeBase64(bytes)); JSONObject jsonObject = new JSONObject(); JSONObject mainjsonObject = new JSONObject(); jsonObject.put("id","121"); jsonObject.put("pdf bytearray",encodedBase64); mainjsonObject.put("PDF details",jsonObject); flag = "" + mainjsonObject; return Response.status(200).entity(flag).build(); } 

my Client:

 String encodedBase64=jsonob.optString("pdf bytearray"); byte[] decodedBytes = Base64.decodeBase64(encodedBase64); System.out.println("decbyte "+decodedBytes); File someFile = new File("C:/Users/acer/Desktop/test.pdf"); OutputStream fos = new FileOutputStream(someFile); fos.write(decodedBytes); fos.flush(); fos.close(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.