我正在尝试将 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 转换为 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的代码与第一个的代码进行了比较,并且有点不同。
我需要从 inputStream 创建一个有效的 pdf 文件。
使用 django-wkhtmltopdf 生成时将 PDF 保存到磁盘
php使用imagick把pdf转图片失败
生成不了pdf
运行也没报错
相关分类