2017-07-26 91 views
0

我試圖從Java中通過webservice返回的字節數組中生成PDf,但PDF無法打開。它顯示了它的損壞,我附上了我的代碼。任何人都可以幫我解決我錯在哪裏?從bytearray創建損壞的PDF

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

以下是從Web服務我的JSON:

{"PDF details": { 
       "id":"121", 
       "pdf bytearray":"[[email protected]" 
       } 
} 

以下是我的web服務代碼中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(); 

} 
+0

您web服務中的'pdf bytearray'無效。您需要修復web服務。 – 1615903

+0

我會附上我的web服務代碼..請檢查prblm是否與它? – Leeza

+0

byte []。toString()不會生成包含字節數組內容的字符串。只是一個包含數組類型的字符串('[B'),後跟'@',後跟一個hashCode('62a58cd')。你應該能夠自己實現一個完整的PDF文件不可能適合10個字符。使用base-64將pdf字節轉換爲可打印的字符串,反之亦然。 –

回答

1

我猜中了與以下在我的web服務chnge:

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

我的客戶端:

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