Escape result of FileLocator.resolve(url)

前端 未结 3 1908
孤独总比滥情好
孤独总比滥情好 2021-02-15 15:31

The method FileLocator.resolve(url) can be used to translate an address bundleentry://something/somewhere/x.txt to a proper file URL for /mnt/foo

相关标签:
3条回答
  • 2021-02-15 15:57

    Two additional notes:

    • FileLocator.resolve indeed resolves an URL, but it does not necessarily return a file:/ URL. In the default case where your bundle is packed (in a .jar), you should use FileLocator.toFileURL, which automatically extracts the resource to a cache if needed.
    • since Eclipse 4.x now includes EMF Common API by default, you can escape the URL more simply with EMF's URI API as follows:

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

    To get the file name, call resolvedUri.toFileString();

    0 讨论(0)
  • 2021-02-15 16:04

    I just found this code:

    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

    The relevant lines indeed help:

    // 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);
    
    0 讨论(0)
  • 2021-02-15 16:18

    From Vogella Blog:

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

    To get the URL, Following can be used:

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

    Also, one can choose the type of Stream/Reader to read image/text.

    0 讨论(0)
提交回复
热议问题