【发布时间】:2013-12-20 18:25:31
【问题描述】:
我有一个带有代码的简单应用程序:
webView.getEngine().load("classpath:data/index.html");
自定义 URLStreamHandler:
public class Handler extends URLStreamHandler {
private final ClassLoader classLoader;
public Handler() {
this.classLoader = getClass().getClassLoader();
}
public Handler(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
protected URLConnection openConnection(URL u) throws IOException {
URL resourceUrl = classLoader.getResource(u.getPath());
if(resourceUrl == null)
throw new IOException("Resource not found: " + u);
return resourceUrl.openConnection();
}
}
安装者:
URL.setURLStreamHandlerFactory(protocol -> {
if(protocol.equals("classpath")) {
return new Handler();
} else {
return null;
}
});
它加载数据/index.html:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div>Hello, World!!!</div>
<img src="download.jpg">
</body>
</html>
但结果图像没有出现。
如何让 WebView 解析像“download.jpg”这样的相对链接?
【问题讨论】: