Java 在部署的应用程序上找不到jar中的图像
我有一个生成pdf报告并在pdf报告上加载图像的应用程序,当我在IDE上运行代码时,效果非常好,但当应用程序部署时,图像不会显示在pdf报告上。图像位于jar文件中,该文件在IDE上是可执行的,但当我将应用程序作为windows服务安装时,在代码中指定的类路径中找不到图像 我声明了这样一个静态字符串变量Java 在部署的应用程序上找不到jar中的图像,java,spring,pdf,pdf-generation,itext,Java,Spring,Pdf,Pdf Generation,Itext,我有一个生成pdf报告并在pdf报告上加载图像的应用程序,当我在IDE上运行代码时,效果非常好,但当应用程序部署时,图像不会显示在pdf报告上。图像位于jar文件中,该文件在IDE上是可执行的,但当我将应用程序作为windows服务安装时,在代码中指定的类路径中找不到图像 我声明了这样一个静态字符串变量 public static final String IMAGES = "classpath:META-INF/images/%s.png"; 在类路径上,我尝试这样指定“classpath*
public static final String IMAGES = "classpath:META-INF/images/%s.png";
在类路径上,我尝试这样指定“classpath*:”
仍然不起作用
我在下面的代码中调用了getImage方法
private void createReportHeader(PdfWriter writer, Document document, String affectedOperation, String timeTaken, String size) throws DocumentException {
log.debug("Adding header info to document page.");
PdfPTable headerTable = iTextUtil.createTable(document.getPageSize().getWidth() - 40, 2);
// add header title & subtitle
Paragraph paragraph = iTextUtil.createDefaultParagraph(AUTHOR + "\n" + "Business Report on " + affectedOperation + "\n");
PdfPCell titleCell = iTextUtil.createBorderlessCell();
titleCell.addElement(paragraph);
headerTable.addCell(titleCell);
headerTable.addCell(iTextUtil.createBorderlessCell());
// add header left cell
Image logo = getImage("logo", LOGOS);
logo.scaleToFit(60, 50);
PdfPCell leftCell = iTextUtil.createBorderlessCell(logo);
// leftCell.addElement(leftParagraph);
headerTable.addCell(leftCell);
// add header right cell
headerTable.addCell(createHeaderRight(timeTaken, size));
Rectangle pageSize = document.getPageSize();
// headerTable.writeSelectedRows(0, -1, 20, document.getPageSize().getHeight() - 10, writer.getDirectContent());
document.add(headerTable);
}
如果在jar中找到图像,该方法将从给定路径检索图像
public Image getImage(String name, String path) {
Image result = null;
try {
File resource = ResourceUtils.getFile(String.format(path, name));
URL url = resource.toURI().toURL();
if (url != null)
result = Image.getInstance(url);
}
catch (IOException | BadElementException e) {
ReportGenerator.log.info("Image file "+name+" not found in path: " +path);
}
return result;
}
我们将非常感谢您的帮助,并提前表示感谢