下面是我用来尝试流回 PDF 的代码。我之前在另一个环境中做过这件事,但由于某种原因,没有调用 StreamingOutput 上的 write() 方法。
没有错误被记录下来,它已经到了在日志中显示“CONT”的地步,所以我知道它至少到达了 StreamingPdfOutput 类的构造函数。
发生的情况是它最终只是向客户端返回 404,即使所有这些代码都已执行(write() 方法除外)。
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@RequestMapping(value = "/ajax/fetchReportPdf", method = RequestMethod.POST)
public StreamingOutput fetchReportPdf(@RequestBody MyRequest request, @Context HttpServletResponse response)
{
LOG.info("start fetchReportPdf");
// AssessmentResponse response = new AssessmentResponse();
try
{
LOG.info("start pdf gen 3");
File f = pdfUtil.generatePdf();
response.setHeader("Content-Length", "" + f.length());
response.setHeader("Content-Disposition", "attachment; filename=\"report.pdf\"");
LOG.info("start response ok NEW: " + f);
return new StreamingPdfOutput(f);
}
catch (Throwable ex)
{
ex.printStackTrace();
LOG.error("ERR", ex);
}
LOG.info("start err build");
return null;
}
private class StreamingPdfOutput implements StreamingOutput
{
File pdfFile;
public StreamingPdfOutput(File f)
{
LOG.info("CONT");
pdfFile = f;
}
@Override
public void write(OutputStream os) throws IOException, WebApplicationException
{
try
{
LOG.info("COPY: " + pdfFile);
FileUtils.copyFile(pdfFile, os);
FileUtils.deleteQuietly(pdfFile);
}
catch (Throwable t)
{
LOG.error("ERR COPY", t);
}
}
}
最佳答案
尝试在 BinaryDataProvider.java 的 isWriteable 中设置断点。我发现在我的案例中,我使用的 Java8 Lambda 没有通过 isWritable 测试。
关于java - Apache CXF 不在 StreamingOutput 类上调用 write() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245578/