方法FileLocator.resolve(url)
可用于将地址bundleentry://something/somewhere/x.txt
转换为/mnt/foo/somewhere/x.txt
的正确文件网址。
但是,https://bugs.eclipse.org/bugs/show_bug.cgi?id=145096也记录了这一点,但网址未被转义。例如,如果包含引用的bundle的Eclipse安装位于包含空格的目录中,则FileLocator.resolve
返回的URL仍包含空格,因此调用url.toURI()
失败。
File
对象
捆绑?作为参考,如果该文件位于包含空格的目录中,则尝试在我的插件的dir
文件中找到目录.jar
时,代码失败:
final IPath pathOfExampleProject = new Path("dir");
final Bundle bundle = Platform.getBundle(AproveIDs.PLUGIN_ID);
final URL url = FileLocator.find(bundle, pathOfExampleProject, null);
final URL url2 = FileLocator.toFileURL(url);
url2.toURI(); // Illegal character in path at index [...]
答案 0 :(得分:7)
我刚刚找到了这段代码:
相关的行确实有帮助:
// We need to use the 3-arg constructor of URI in order to properly escape file system chars.
URI resolvedUri = new URI(resolvedUrl.getProtocol(), resolvedUrl.getPath(), null);
答案 1 :(得分:1)
另外两个注意事项:
URI resolvedUri = URI.createFileURI(resolved.getPath());
要获取文件名,请致电resolvedUri.toFileString();
答案 2 :(得分:0)
URL url;
try {
url = new
URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
InputStream inputStream = url.openConnection().getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
要获取网址,可以使用以下网址:
Bundle thisBundle = FrameworkUtil.getBundle(getClass());
URL fileURL = thisBundle.getEntry("<relative_file_path_from_project_root");
另外,可以选择Stream / Reader的类型来读取图像/文本。