蜗居的地主 2020-09-25 22:33 采纳率: 0%
浏览 697

接口返回的PDF数据如何保存为PDF文件

RestTemplate请求接口返回PDF数据,如下面第二段,生成pdf无法打开,请问代码有啥问题吗,或者有别的实现方式

        RestTemplate client = new RestTemplate();
        String url = "接口地址";

        ResponseEntity response = client.postForEntity(url, requertXml, String.class);
        String rs = response.getBody().toString();
        System.out.println(rs);

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream("G:\\temp\\data.pdf");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        bos = new BufferedOutputStream(fos);

        try {
            bos.write(rs.getBytes());
            bos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
%PDF-1.4

中间省略

<</Root 59 0 R/Prev 548069/ID [<17e73f673b8c9ec03dd54ad7b2d26a39><aa37e40b3fc5f15a1834d45d4a77c051>]/Encrypt 60 0 R/Info 1 0 R/Size 70>>
%iText-5.5.1
startxref
568719
%%EOF
  • 写回答

3条回答 默认 最新

  • threenewbee 2020-09-25 23:26
    关注

    %PDF-1.4
    这个开头就说明你的字节流就是pdf了
    设置下content-type看看,把下载的文件另存为修改后缀为pdf

    评论

报告相同问题?