我的项目使用itext7来创建PDF文件。当我从日食发射时,一切都很完美。当我把它打包成jar时,一切正常,直到我想要创建一个PDF。然后我得到:
线程“JavaFX Application Thread”中的异常com.itextpdf.io.IOException:I / O异常。 .....
引起:java.io.FileNotFoundException:C:\ Users \ puser \ eclipse-workspace \ Document \ target \ SE001-0.1.1-SNAPSHOT.jar \ img \ Safety.png(系统找不到指定的路径)
项目文件夹将图像保存在src/main/resources/img
。一旦jar被创建,它只是在根源上有/img
。这意味着您不能只指定直接路径,因为它在制作jar时会发生变化。 JavaFX图像可以正常工作..
Image user = new Image(getClass().getResourceAsStream("/img/Document.png"));
使用它与itext7不起作用,因为ImageDataFactory.create()
正在寻找byte [],这是一个输入流。
现在尝试使用:
Image safetyImage = new Image(ImageDataFactory.create(System.getProperty("user.dir") + "/img/Safety.png"));
不起作用,因为Jar不在路径内。
我可以用什么来指向jar中的图像文件并将其与ext7一起使用?
mkl是对的,谢谢!
我创建了一个实用程序方法来将输入流转换为字节数组。
public static byte[] toByteArray(InputStream in) throws IOException {
//InputStream is = new BufferedInputStream(System.in);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len;
// read bytes from the input stream and store them in buffer
while ((len = in.read(buffer)) != -1) {
// write bytes from the buffer into output stream
os.write(buffer, 0, len);
}
return os.toByteArray();
}
然后我在ImageDataFactory.create()方法中使用了该实用程序。
Image safetyImage = new Image(ImageDataFactory.create(toByteArray(getClass().getResourceAsStream("/img/Safety.png"))));