从OutputStream的下载PDF文件

问题描述 投票:2回答:2

我用这种方法工作获取文件

@GetMapping(value = "/test", produces = MediaType.APPLICATION_PDF_VALUE)
public String test() throws FOPException, IOException { 

    SisDocuments document = documentRepository.getOne(Long.valueOf("801859"));

    documentService.constructDocumentById(document);        


    return "/connexion";
}

@Override
public void constructDocumentById(SisDocuments document) {

    try {

        File inputFile = new File("input.txt");

        File xsltfile = new File(path + "dz/sis-fop.xsl");
        FopFactory fopFactory = FopFactory.newInstance();
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

        ByteArrayOutputStream bout = new ByteArrayOutputStream();


        OutputStream out;
        out = new java.io.FileOutputStream("employee.pdf");

        try {
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(xsltfile);
            Transformer transformer = factory.newTransformer(xslt);

            File fXmlFile = new File(path +  "dz/my-file.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            Result res = new SAXResult(fop.getDefaultHandler());

            DOMSource source = new DOMSource(doc);

            transformer.transform(source, res);



        } finally {
        }

    } catch (Exception e) {
        System.err.println(" r " + e.getMessage());
        e.printStackTrace();
    }

}

此方法在我的项目目录中创建一个文件,而不是创建它,我想下载

java spring rest output
2个回答
0
投票

造成这种情况的典型方式是让你的文件InputStream并将其写入到OutputStreamResponse

@GetMapping(value = "/test/{idDocument}", produces = MediaType.APPLICATION_PDF_VALUE)
public void test(@PathVariable("idDocument") String idDocument, HttpServletRequest request,
        HttpServletResponse response) throws FOPException, IOException {

    SisDocuments document = documentRepository.getOne(Long.valueOf(idDocument));

    documentService.constructDocumentById(document);

    try {
        // // get your file as InputStream
        File f = new File("employee.pdf");
        InputStream is = new FileInputStream(f);

        org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());

        // copy it to response's OutputStream
        response.addHeader("Content-disposition", "attachment; filename=" + "employee.pdf");
        response.setContentType("application/pdf");
        response.flushBuffer();
    } catch (IOException ex) {
        throw new RuntimeException("IOError writing file to output stream");
    }

}

0
投票

你的控制器方法不能返回String,实际上它必须没有返回:)要流文件,你有你的内容直接写在javax.servlet.http.HttpServletResponse - outputstream,例如:

HttpServletResponse response;
Files.copy( yourPath, response.getOutputStream() );
© www.soinside.com 2019 - 2024. All rights reserved.