8

このメソッドを使用して、アドレスをの適切なファイルURLにFileLocator.resolve(url)変換できます。bundleentry://something/somewhere/x.txt/mnt/foo/somewhere/x.txt

ただし、 https://bugs.eclipse.org/bugs/show_bug.cgi?id = 145096にも記載されているように、URLはエスケープされません。例として、参照されるバンドルを含むEclipseインストールがスペースを含むディレクトリにある場合、によって返されるURLにはFileLocator.resolveまだスペースが含まれているため、呼び出しurl.toURI()は失敗します。

  • URLに必要なすべての文字を手動でエスケープするにはどうすればよいですか?
  • 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 [...]
4

3 に答える 3

7

私はちょうどこのコードを見つけました:

http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/ core / internal / model / BundledSystemLibrary.java?r = 2057

関連する行は確かに役立ちます:

// 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);
于 2013-02-03T20:50:14.870 に答える
1

2つの追加の注意:

  • FileLocator.resolveは実際にURLを解決しますが、必ずしもfile:/URLを返すとは限りません。バンドルが(.jarに)パックされているデフォルトの場合、FileLocator.toFileURLを使用する必要があります。これにより、必要に応じてリソースがキャッシュに自動的に抽出されます。
  • Eclipse4.xにはデフォルトでEMFCommonAPIが含まれているため、次のようにEMFのURIAPIを使用してURLをより簡単にエスケープできます。

URI resolvedUri = URI.createFileURI(resolved.getPath());

ファイル名を取得するには、resolvedUri.toFileString();

于 2014-10-27T16:18:23.923 に答える
0

Vogellaブログから:

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();
}

URLを取得するには、次を使用できます。

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

また、画像/テキストを読み取るストリーム/リーダーのタイプを選択できます。

于 2018-05-08T13:19:38.550 に答える