使用 Streams 生成 PDF

Generate a PDF using Streams

我正在尝试将 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[] 并将其写入新文件时出现问题。 我有这两种方法:

将流转换为字节[]:

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 文件。

你有两个问题:

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

  2. 在此处将 String 转换为字节时丢失了数据: 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();
    }
}
}