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

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

问题描述

我需要从JSON对象生成PDF文件,并且能够使用Apache FOP和Java生成PDF文档。但是我想以 ByteArrayOutputStream 的形式返回PDF文件,然后我必须像这样 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中使用的BAOS,这是假定的字节数组输出流正在通过某些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天全站免登陆