Java 使用流避免临时文件

Java 使用流避免临时文件,java,stream,apache-fop,Java,Stream,Apache Fop,我正在尝试使用Apache库FOP创建PDF文档。它工作得很好,但我必须创建一个临时文件来存储一个中间文件(test.fop) 以下是我的功能: @RequestMapping(value = "/pdf") public void showPdfReport(OutputStream pdfOutStream) throws Exception { // Setup FopFactory fopFactory = FopFactory.newInstance(); Tr

我正在尝试使用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存储在流或任何缓冲区而不是文件中?

当然,您可以将任何读取器或InputStream传递给

您可以使用或,例如。

FOP网站上有一个详细说明。XSLT转换发出的SAX事件直接输入FOP。文件或内存中没有缓冲。

请查看