我正在尝试将资源打包到一个 jar 中,但我无法让 Flying Saucer 在类路径上找到 css -我无法轻松构建一个 URL 来无缝解决这个问题。
飞碟是否有办法在类路径上指定资源包来解析项目和图像?
注意:我在一个没有文件系统写入权限的 webstart 应用程序中运行它,所以 jar 扩展并不是一个真正的选择。
我正在尝试将资源打包到一个 jar 中,但我无法让 Flying Saucer 在类路径上找到 css -我无法轻松构建一个 URL 来无缝解决这个问题。
飞碟是否有办法在类路径上指定资源包来解析项目和图像?
注意:我在一个没有文件系统写入权限的 webstart 应用程序中运行它,所以 jar 扩展并不是一个真正的选择。
你应该实现一个你提供给 XHTMLPanel 的 UserAgentCallback,像这样:
private static class UAC extends NaiveUserAgent {
@Override
public String resolveURI(String uri) {
return uri;
}
@Override
protected InputStream resolveAndOpenStream(String uri) {
java.io.InputStream is = null;
URL url = UAC.class.getResource(uri);
if (url == null) {
XRLog.load("Didn't find resource [" + uri + "].");
return null;
}
try {
is = url.openStream();
}
catch (java.net.MalformedURLException e) {
XRLog.exception("bad URL given: " + uri, e);
}
catch (java.io.FileNotFoundException e) {
XRLog.exception("item at URI " + uri + " not found");
}
catch (java.io.IOException e) {
XRLog.exception("IO problem for " + uri, e);
}
return is;
}
}
XHTMLPanel panel = new XHTMLPanel(new UAC());
我的解决方案是
private static class UserAgentCallback extends ITextUserAgent {
public UserAgentCallback(ITextOutputDevice outputDevice, SharedContext sharedContext) {
super(outputDevice);
setSharedContext(sharedContext);
}
@Override
public String resolveURI(String uri) {
return uri;
}
@Override
protected InputStream resolveAndOpenStream(String uri) {
java.io.InputStream is = null;
URL url = null;
try {
url = new ClassPathResource("/META-INF/pdfTemplates/" + uri).getURL();
} catch (IOException e) {
XRLog.exception("bad URL given: " + uri, e);
}
if (url == null) {
XRLog.load("Didn't find resource [" + uri + "].");
return null;
}
try {
is = url.openStream();
} catch (java.net.MalformedURLException e) {
XRLog.exception("bad URL given: " + uri, e);
} catch (java.io.FileNotFoundException e) {
XRLog.exception("item at URI " + uri + " not found");
} catch (java.io.IOException e) {
XRLog.exception("IO problem for " + uri, e);
}
return is;
}
}
和调用:
renderer.getSharedContext()
.setUserAgentCallback(new UserAgentCallback(renderer.getOutputDevice(), renderer.getSharedContext()));
似乎飞碟没有在类路径上指定资源的方法,所以我通过在链接问题上创建一个类路径来解决:协议 url 处理程序
这个问题的某些前提似乎是无效的。写完自己的classpath URL loader后,发现需要<all-permissions/>
在jnlp中请求才能使用URL.setURLStreamHandlerFactory()
. 事实上,您需要请求所有权限才能做任何花哨的事情(即使您只是在修改自己的沙盒)。在此处查看完整列表。
简而言之,这意味着我能够将文件提取到操作系统。但是现在有一个类路径加载器很好......