从字节数组创建损坏的PDF文件

3

我试图从通过网络服务返回的字节数组中生成PDF,但是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":"[B@62a58cd"
                 }
}

以下是我的webservice代码,它会在json中输出bytearray:
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();

}

你的网络服务中的 pdf bytearray 不合法,你需要修复该网络服务。 - 1615903
我会附上我的 Web 服务代码,请检查一下是否有问题? - Leeza
1
byte[].toString() 不会生成包含字节数组内容的字符串,而是只包含数组类型([B)后跟 @ 再跟上哈希码(62a58cd)的字符串。你应该能够自己意识到整个 PDF 文档不可能适合 10 个字符。使用 base-64 将 pdf 字节转换为可打印字符串,反之亦然。 - JB Nizet
3
我已经说过了。请花几分钟时间阅读和消化我的评论,思考并进行一些研究。在此期间,请避免在 JSON 对象的键中使用空格和随意的大写字母。请使用 Java 变量命名规范: pdfDetails 等。 - JB Nizet
1个回答

2
我在我的Web服务中进行了以下更改,现在已经正确了:

我在我的Web服务中进行了以下更改,现在已经正确了:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接