【发布时间】:2025-12-26 09:50:11
【问题描述】:
我正在尝试将我在服务器中生成的 PDF 文件发送到我的 Jersey 应用程序中的客户端。生成很好,我在本地机器上创建了文件,没有问题。
当我尝试通过流发送它时出现问题。当我尝试读取输出的文件时,它完全是空的(有时,它只是损坏了,相同的文件代码相同,但文件被损坏而不是空白)但是,页面的数量是正确的,这真的很奇怪。然后我尝试使用流创建一个本地文件,它工作得很好,所以我很确定问题不在于我放入流中的内容,而是我将其发送到客户端的方法。我还尝试了不同的、完全正常的 pdf,但我遇到了同样的问题。
一些代码示例
@Path("/produiretest/{id}")
@GET
@Produces("application/pdf")
public Response rapportStreamTest(@PathParam("id") Long id) throws Exception {
final StreamingOutput file = manager.produireRapportStreamingTest(id);
Response.ResponseBuilder response = Response.ok(file);
response.header("Content-Disposition", "attachment; filename=\"" + "previsualisationFicheSignaletique.pdf");
return response.build();
}
StreamingOutput 实现(即使我认为这是正确的)
StreamingOutput outStream = new StreamingOutput() {
@Override
public void write(OutputStream outputStream) {
try {
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
outputStream.flush();
outputStream.close();
}
catch (Exception ex) {
}
}
};
return outStream;
戳服务器时得到的响应示例,显然已损坏。
%PDF-1.4 %���� 5 0 对象 流 x����1 ��g ?���� 端流 结束对象 6 0 对象 ]/Intent/Perceptual/Subtype/Image/Height 600/Filter/FlateDecode/Type/XObject/Width 600/SMask 5 0 R/Length 23357/BitsPerComponent 8>>stream x���{pU��>pLB¥E@����Bu8����!@% �=P�H)j�NsxAD8"IA��$��i�r�Ŋ0@�~ ������7@B.{��k=k������������I����_"����������u͚5K... ... ___��!�s�G�O�p8v=DD�6�֯��{�����cƌy��G�ao�>U�n������
现在不要真的去哪里,任何指针都会很感激。
【问题讨论】: