You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中无法正确处理并写入Microsoft Graph返回的PDF文件

问题:Microsoft Graph转换PDF后Java/Groovy写入文件空白但页数正常

我通过Microsoft Graph将docx文件转换为PDF,服务返回Content-Type为application/pdf的二进制数据。Postman中能正常查看该PDF,但用Groovy/Grails(Java也适用)代码写入文件后,PDF页数正确但所有页面全空白。

注意到代码生成的文件大小(1487 KB)比Postman下载的(1563 KB)小,但两者返回的content-length都是1599780,显然是处理二进制数据时出了问题。

响应体示例

%PDF-1.7
%����
1 0 obj
<<Type/Catalog/Pages 2 0 R/Lang(en) /StructTreeRoot 466 0 R/Outlines 420 0 R/MarkInfo<<Marked true>>/Metadata 3921 0 R/ViewerPreferences 3922 0 R>>
endobj
2 0 obj
<<Type/Pages/Count 135/Kids[ 3 0 R 15 0 R 19 0 R 25 0 R 26 0 R 29 0 R 31 0 R 32 0 R 33 0 R 36 0 R 37 0 R 38 0 R 40 0 R 42 0 R 43 0 R 44 0 R 45 0 R 46 0 R 47 0 R 48 0 R 49 0 R 50 0 R 51 0 R 52 0 R 53 0 R 54 0 R 55 0 R 56 0 R 57 0 R 58 0 R 59 0 R 60 0 R 61 0 R 62 0 R 63 0 R 64 0 R 65 0 R 66 0 R 67 0 R 68 0 R 69 0 R 70 0 R 71 0 R 72 0 R 73 0 R 74 0 R 75 0 R 76 0 R 77 0 R 78 0 R 79 0 R 80 0 R 81 0 R 82 0 R 83 0 R 84 0 R 85 0 R 86 0 R 87 0 R 88 0 R 89 0 R 90 0 R 91 0 R 92 0 R 93 0 R 94 0 R 95 0 R 96 0 R 97 0 R 98 0 R 99 0 R 100 0 R 101 0 R 102 0 R 103 0 R 104 0 R 105 0 R 106 0 R 107 0 R 108 0 R 109 0 R 110 0 R 111 0 R 112 0 R 113 0 R 114 0 R 115 0 R 116 0 R 117 0 R 118 0 R 119 0 R 120 0 R 121 0 R 122 0 R 123 0 R 124 0 R 125 0 R 126 0 R 127 0 R 128 0 R 129 0 R 130 0 R 131 0 R 132 0 R 133 0 R 134 0 R 135 0 R 136 0 R 137 0 R 138 0 R 139 0 R 140 0 R 141 0 R 142 0 R 143 0 R 144 0 R 145 0 R 146 0 R 147 0 R 148 0 R 149 0 R 150 0 R 151 0 R 152 0 R 153 0 R 154 0 R 155 0 R 156 0 R 157 0 R 159 0 R 161 0 R 166 0 R 170 0 R 171 0 R 172 0 R] >>
endobj
3 0 obj
<<Type/Page/Parent 2 0 R/Resources<<Font<<F1 5 0 R/F2 9 0 R>>/ExtGState<<GS7 7 0 R/GS8 8 0 R>>/XObject<<Image11 11 0 R/Image13 13 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<<Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<<Filter/FlateDecode/Length 702>>
stream
x���Qo�0�ߑ��h��`� U����2-R�0���Mi�D�����~g�6�   +Mh"Y`|�w�;�=YU�]<����=��x~��•�=��{/�<��"wg�7����ķ�������-�Q�%h!����༞\%�����y�0�-��c���Иk4��1����|�a�`[,�`}vn[W�k����)��|>�̣�o�u��h+��5��p"D������N[�f�ˏ�/�2��

我的代码

response= rest.get(url){
    header "Authorization", "Bearer "+updateBulkPublishPdfDetailsData.sharepointAccessToken
}
RestResponse resp=sharepointUploadSummaryResponse.getResponseEntity()
if(resp.getHeaders().getContentType().toString()=="application/pdf"){
    String pdfString=resp.body;
    FileOutputStream out = new FileOutputStream(new File("C:\\Users\\faizanahemad.shaikh\\Downloads\\abcd.pdf"));
    out.write(pdfString.getBytes());
    out.flush();
    out.close();
}
解决方案

问题根源是将二进制PDF数据当作字符串处理,导致编码转换丢失了原始二进制信息。字符串是基于字符编码的,而PDF是二进制文件,直接转字符串再转回字节数组会破坏非文本字节(比如stream部分的压缩数据)。

正确处理方式:直接操作字节数组

Groovy/Grails 代码修改

直接将响应体读取为字节数组,避免字符串转换:

response= rest.get(url){
    header "Authorization", "Bearer "+updateBulkPublishPdfDetailsData.sharepointAccessToken
    accept("application/pdf")
    responseType(byte[].class)
}
RestResponse resp=sharepointUploadSummaryResponse.getResponseEntity()
if("application/pdf".equals(resp.getHeaders().getContentType().toString())){
    byte[] pdfBytes = resp.body
    FileOutputStream out = new FileOutputStream(new File("C:\\Users\\faizanahemad.shaikh\\Downloads\\abcd.pdf"));
    out.write(pdfBytes);
    out.flush();
    out.close();
}

Java 等效实现

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + sharepointAccessToken);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_PDF));
HttpEntity<String> entity = new HttpEntity<>(headers);

ResponseEntity<byte[]> resp = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
if(MediaType.APPLICATION_PDF.equals(resp.getHeaders().getContentType())){
    FileOutputStream out = new FileOutputStream(new File("C:\\Users\\faizanahemad.shaikh\\Downloads\\abcd.pdf"));
    out.write(resp.getBody());
    out.flush();
    out.close();
}

验证要点

  • 写入后的文件大小需和content-length一致(1599780字节)
  • 用PDF阅读器打开时,检查是否能正常显示内容而非空白页

内容的提问来源于stack exchange,提问作者Faizan Shaikh Sarkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 14:17:08