以下是我用来尝试流回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);
}
}
}
答案 0 :(得分:0)
尝试在BinaryDataProvider.java中使用breakpointing的isWriteable。在我的案例中,我发现我使用的Java8 Lambda未通过isWritable测试。