Eclipse FileLocator.resolve的转义结果(url)

Eclipse FileLocator.resolve的转义结果(url),eclipse,eclipse-plugin,Eclipse,Eclipse Plugin,方法FileLocator.resolve(url)可用于翻译地址bundleentry://something/somewhere/x.txt到/mnt/foo/somewhere/x.txt的正确文件URL 但是,也记录在中,URL不会转义。例如,如果包含引用包的Eclipse安装在包含空格的目录中,则FileLocator.resolve返回的URL仍然包含空格,因此调用URL.toURI()失败 如何手动转义URL中所有必需的字符 如何根据相对于当前文件的路径获取文件对象 包裹 作为

方法
FileLocator.resolve(url)
可用于翻译地址
bundleentry://something/somewhere/x.txt
/mnt/foo/somewhere/x.txt的正确文件URL

但是,也记录在中,URL不会转义。例如,如果包含引用包的Eclipse安装在包含空格的目录中,则
FileLocator.resolve
返回的URL仍然包含空格,因此调用
URL.toURI()
失败

  • 如何手动转义URL中所有必需的字符
  • 如何根据相对于当前文件的路径获取
    文件
    对象 包裹
作为参考,以下是在我的插件的
.jar
文件中查找目录
dir
时失败的代码,如果该文件位于包含空格的目录中:

final IPath pathOfExampleProject=新路径(“dir”);
最终Bundle=Platform.getBundle(AproveIDs.PLUGIN\u ID);
最终URL=FileLocator.find(bundle,pathOfExampleProject,null);
最终URL url2=FileLocator.toFileURL(URL);
url2.toURI();//索引[…]处的路径中存在非法字符

我刚刚找到了以下代码:

相关线路确实有助于:

// 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);
另外两项说明:

  • FileLocator.resolve确实解析URL,但它不一定返回文件:/URL。在打包包的默认情况下(在.jar中),应该使用FileLocator.toFileURL,如果需要,它会自动将资源提取到缓存中
  • 由于Eclipse 4.x现在默认包含EMF公共API,因此可以使用EMF的URI API更简单地转义URL,如下所示:
URI resolvedUri=URI.createFileURI(resolved.getPath())

要获取文件名,请调用
resolvedUri.toFileString()

来自

要获取URL,可以使用以下内容:

Bundle thisBundle = FrameworkUtil.getBundle(getClass());
URL fileURL = thisBundle.getEntry("<relative_file_path_from_project_root");
Bundle thisBundle=FrameworkUtil.getBundle(getClass());
URL fileURL=thisBundle.getEntry(“
Bundle thisBundle = FrameworkUtil.getBundle(getClass());
URL fileURL = thisBundle.getEntry("<relative_file_path_from_project_root");