返回为 ByteArrayOutputStream 而不是 FileOutputStream [英] Return as ByteArrayOutputStream instead of FileOutputStream

查看:32
本文介绍了返回为 ByteArrayOutputStream 而不是 FileOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 JSON 对象生成 PDF 文件,并且能够使用 Apache FOP 和 Java 生成 PDF 文档.但我想将 PDF 文件作为 ByteArrayOutputStream 返回,然后我必须像这样编码 Base64.getEncoder().encodeToString(baos.toByteArray()).

I have a requirement to generate PDF file from JSON Objects and am able to produce PDF document using Apache FOP and Java. But i want to return the PDF file as ByteArrayOutputStream and then i have to encode like this Base64.getEncoder().encodeToString(baos.toByteArray()).

请找到以下能够生成 PDF 文件的代码,而不是 FileOutputStream 我想返回为 ByteArrayOutputStream.

Please find the below code where am able to generate PDF file, instead of FileOutputStream i want to return as ByteArrayOutputStream.

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        Transformer xslfoTransformer;
        try
        {
            xslfoTransformer = getTransformer(transformSource);
                Fop fop;
            try
            {
                fop = fopFactory.newFop
                    (MimeConstants.MIME_PDF, outStream);

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

                        ITextRenderer renderer = new ITextRenderer();

                try
                {
                    xslfoTransformer.transform(source, res);
                    File pdffile = new File("Result.pdf");
                    OutputStream out = new java.io.FileOutputStream(pdffile);
                                out = new java.io.BufferedOutputStream(out);
                                FileOutputStream str = new FileOutputStream(pdffile);
                                str.write(outStream.toByteArray());
                                str.close();
                                out.close();

                }
                catch (TransformerException e) {
                    throw e;
                }
            }
            catch (FOPException e) {
                throw e;
            }
        }
        catch (TransformerConfigurationException e)
        {
            throw e;
        }
        catch (TransformerFactoryConfigurationError e)
        {
            throw e;
        }

推荐答案

我对你的问题有点困惑.我没有使用过 Apache Fop,但会尝试回答这个问题.

I am slightly confused to what you are asking. I have not used Apache Fop, but will try answer this question.

如果您想将 PDF 文件作为字节数组读取,请改用输入流.

If you want to read PDF file as byte array use input streams instead.

但是,如果您想使用写入字节的 ByteArrayOutputStream,您基本上回答了您自己的问题,请尝试使用您最初创建的现有 BAOS,并且您在 FileOutputStream 中使用,假设字节数组输出流正在通过某些 InputStream 读取字节或其他一些来源.第二个假设是 BAOS 和 FOS 能够正确编写您正在谈论的 PDF 文件.你可以简单地做:

But if you want to use ByteArrayOutputStream that is writing bytes you basically answered your own question, try using existing BAOS that you created initially and you are using in FileOutputStream, that's assuming the byte array output stream is reading bytes via some InputStream or some other source. Second assumption is that BAOS and FOS were able to properly write PDF file you were talking about. You can simply do:

byte[] b = outStream.toByteArray();
String str = Base64.getEncoder().encodeToString(b); 

这篇关于返回为 ByteArrayOutputStream 而不是 FileOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆