我試圖用Apache庫FOP創建一個PDF文檔。它運行良好,但我必須創建一個臨時文件來存儲中間文件(test.fop)。使用流來避免臨時文件
這裏是我的功能:
@RequestMapping(value = "/pdf")
public void showPdfReport(OutputStream pdfOutStream) throws Exception
{
// Setup
FopFactory fopFactory = FopFactory.newInstance();
TransformerFactory factory = TransformerFactory.newInstance();
// Transformation XML to XML-FO
StreamSource xsltSource = new StreamSource(servletContext.getResourceAsStream("resources/xsl/pdfTemplate.fo"));
Transformer transformer = factory.newTransformer(xsltSource);
OutputStream fopOutStream = new BufferedOutputStream(new FileOutputStream(new File(
"C:/Temp/tests/test.fop")));
StreamSource xmlSource = new StreamSource(servletContext.getResourceAsStream("resources/xsl/test.xml"));
StreamResult fopResult = new StreamResult(fopOutStream);
transformer.transform(xmlSource, fopResult);
// Transformation XSL-FO to PDF
Source fopSrc = new StreamSource(new File("C:/Temp/tests/test.fop"));
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, pdfOutStream);
Result res = new SAXResult(fop.getDefaultHandler());
transformer = factory.newTransformer();
transformer.transform(fopSrc, res);
}
是否有可能test.fop存儲在流或任何緩衝,而不是一個文件?
看看[BufferedOutputStream](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html) – McDowell