【问题标题】:Generate a PDF using Streams [duplicate]使用 Streams 生成 PDF [重复]
【发布时间】:2019-03-03 12:08:39
【问题描述】:

我正在尝试将 InputStream 转换为字节数组以将其写入文件,以生成 PDF。

我有一个带有 PDF url 的文件类型,并且有了它的 inputStream。

File fichero_pdf = new File("C:/Users/agp2/Desktop/PDF_TRIAXE.pdf");
InputStream stream4 = new FileInputStream(fichero_pdf);

直到这里一切都很完美,当我尝试将此 InputStream 转换为 byte[] 并将其写入新文件时出现问题。 我有这两种方法:

将 Stream 转换为字节[]:

private static byte[] getArrayFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();


    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString().getBytes();

}

在新文件中写入字节[]:

...

File file=new File(dto.getTitulo());
  InputStream stream=dto.getContenido();      
               byte[] array=getStringFromInputStream(stream);
               OutputStream salida=new FileOutputStream(file);
               salida.write(array);
               salida.close();
               stream.close();
               helper.addAttachment(file.getName(), file);
           }
            mailSender.send(message);
...

电子邮件发送得很完美,但是当我无法打开 .pdf 时。 另外,我将新 pdf 的代码与第一个 pdf 的代码进行了比较,并且有点不同。

我需要从 inputStream 创建一个有效的 pdf 文件。

【问题讨论】:

    标签: java spring pdf inputstream


    【解决方案1】:

    你有两个问题:

    1. 您正在尝试将字节读取为字符串,但您不必这样做。在您的情况下,您应该使用字节流(FileInputStream,BufferedInputStream),而不是字符流(InputStreamReader,BufferedReader)。

    2. 在此处将字符串转换为字节时会丢失数据: return sb.toString().getBytes();

    我想建议您使用 java 7 try-with-resources 而不是 try-catch-finally。 您可以使用 ByteArrayOutputStream 将整个文件读取到字节数组中。

    示例代码执行以下操作:

    1. getArrayFromInputStream() - 将所有文件字节读取到字节数组

    2. writeContent() - 将内容写入新文件,在我的示例中为 pdf_sample2.pdf

    例子:

    public class ReadAllBytes {
    
    // as example - write to resources folder
    private static String DIR = "src\\main\\resources\\";
    
    public static void main(String[] args) throws IOException {
        try {
            byte[] fileAsBytes = getArrayFromInputStream(new FileInputStream(new File(DIR + "pdf-sample.pdf")));
            writeContent(fileAsBytes, DIR + "pdf_sample2.pdf");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    private static byte[] getArrayFromInputStream(InputStream inputStream) throws IOException {
        byte[] bytes;
        byte[] buffer = new byte[1024];
        try(BufferedInputStream is = new BufferedInputStream(inputStream)){
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int length;
            while ((length = is.read(buffer)) > -1 ) {
                bos.write(buffer, 0, length);
            }
            bos.flush();
            bytes = bos.toByteArray();
        }
        return bytes;
    }
    
    private static void writeContent(byte[] content, String fileToWriteTo) throws IOException {
        File file = new File(fileToWriteTo);
        try(BufferedOutputStream salida = new BufferedOutputStream(new FileOutputStream(file))){
            salida.write(content);
            salida.flush();
        }
    }
    }
    

    【讨论】:

    • 它成功了,这是一个愚蠢的失败。谢谢!
    • 酷 =) 很高兴为您提供帮助)
    • 我的pdf是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2011-03-22
    相关资源
    最近更新 更多