java - Apache FOP : Image not found after packing resources into jar

标签 java xml xslt apache-fop

我的 Apache FOP 应用程序有问题。

一切正常,直到我开始将外部文件(xsl、ttf 等)从外部工作目录打包到应用程序的 jar 文件中。从那时起,不再加载任何图像,我收到以下错误:

FOUserAgent - Image not available. URI: data:image/jpeg;base64,.... 
Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is 
not supported. No ImagePreloader found for data:image/jpeg;base64,...(No context info available)
org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for data:image/jpeg;base64

在 xsl 文件中,图像嵌入了:

<fo:external-graphic xsl:use-attribute-sets="helpicon" 
  fox:alt-text="helpicon" 
  src="url('data:image/jpg;base64,...I'm the base64 encoded image data...')">   
</fo:external-graphic>

当我将文件打包到 jar 中时,为什么图像不再工作了?图像作为 base64 字符串嵌入到 xsl 文件中,因此图像数据必须可用...

谢谢:)


编辑 1 我将 FopFactory 基本 url 设置为:

    String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    String decodedPath = URLDecoder.decode(path, "UTF-8");
    File jar = new File(decodedPath);

    fopFactory.setBaseURL(jar.getParent());

这是我的 ClasspathUriResolver:

public class ClasspathUriResolver implements URIResolver {

@Override
public Source resolve(String fileName, String base) throws TransformerException {

    System.out.print(" RESOLVING: " + fileName);

    URL url = getClass().getClassLoader().getResource(fileName);
    StreamSource jarFileSS = new StreamSource();

    System.out.println(" TO: " + url.toString());

    try {
        InputStream jarfileIS = url.openStream();
        jarFileSS.setInputStream(jarfileIS);
    } catch (IOException ioExp) {
        throw new TransformerException(ioExp);
    }
    return jarFileSS;
}

我检查了 ClasspathUriResolver 的日志,包含的文件(字体、样式表)得到正确解析。

最佳答案

我已经将 jdk 版本更新到 1.7.71,现在一切正常。谢谢:)

关于java - Apache FOP : Image not found after packing resources into jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708992/

相关文章:

java - XML 作为数据存储。插入、移除、删除

function - 如何创建一个 xsl :function that returns a boolean

java - 从字符串中选择大写字母并将其转换为字符数组

java - 如何拆分文本文件中的每条数据并将它们放回 Java GUI 表单上各自的文本字段中?

java - 使用 NumberUtils.INTEGER_ONE 和其他此类实用程序的好处

xslt - 我可以更改 XSLT 1.0 以将负半数向下舍入而不是向上舍入吗?即-2.5到-3

xml - 展平 XSLT 中的深层嵌套结构

java - 列出不同用户的 Google 日历 Activity

java - XML 架构 : replacing imports with corresponding schema

mysql - 如何在 mysql 中为此 xml 创建一个表,以及创建表后如何导入?