如何从多部分文件中获取文件,以便可以创建PDImageXObject?

问题描述

我正在使用apache pdfBox从图像创建pdf。我从表单提交中获得图像。我将其转换为BufferedImage。我需要从该图像创建PDImageXObject。有没有办法将此文件转换为File对象,以便可以使用PDImageXObject.createFromFileByContent方法

 def f = request.getFile('file')

 InputStream inputStream = f.getInputStream()

 BufferedImage bimg = ImageIO.read(inputStream);
 float width = bimg.getWidth();
 float height = bimg.getHeight();

enter image description here

我非常感谢您的见解。

解决方法

如果您坚持要使用本地文件(而不是更新PDFBox版本,这是最佳做法),请执行以下代码:

Path tempPath = Files.createTempFile("pdfbox",null);
Files.copy(inputStream,tempPath,StandardCopyOption.REPLACE_EXISTING);

System.out.println(tempPath);

// do stuff with that file

// delete when done        
Files.delete(tempPath);

使用tempPath.toFile()将Path对象转换为File对象。