【问题标题】:Download PDF file from Outputstream从 Outputstream 下载 PDF 文件
【发布时间】:2019-02-07 14:39:32
【问题描述】:

我正在使用这种方法来获取文件

@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


    【解决方案1】:

    这样做的典型方法是将您的文件作为InputStream 并将其写入ResponseOutputStream

    @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");
        }
    
    }
    

    【讨论】:

    • 您的方法必须返回一个String,但我在您的方法中看不到任何return
    • 那是一个错字。谢谢@HalayemAnis
    • 您的解决方案是显示文档内容而不是下载文档
    • 只需为此添加正确的响应标头。 [查看更新的答案]
    • 同样问题的朋友
    【解决方案2】:

    你的控制器方法不能返回String,事实上它不能返回任何东西:) 要流式传输您的文件,您必须将内容直接写入javax.servlet.http.HttpServletResponse - outputstream,例如:

    HttpServletResponse response;
    Files.copy( yourPath, response.getOutputStream() );
    

    【讨论】:

    • 什么是文件的导入?!?
    • 一个标准的 Java 1.7+ 实用程序文件,它只是一个关于如何在输出流中复制文件内容的示例,顺便说一句,为什么 => ?!?
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2014-03-30
    • 2020-11-04
    相关资源
    最近更新 更多