我有一个生成pdf报告并在pdf报告上加载图像的应用程序,当我在IDE上运行代码完美运行时,但是当部署应用程序时,图像不会出现在pdf报告上。图像位于可在IDE上执行的jar文件中,但是当我将应用程序安装为Windows服务时,在代码上指定的类路径中找不到图像。
我已经声明了一个像这样的静态字符串变量
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;
}
提前感谢您的帮助。