public static void main(String[] args) throws Exception{ // SpringApplication.run(MarketCollectApplication.class,args); URI uri = MarketCollectApplication.class.getClassLoader().getResource("conf/sh.txt").toURI(); FileSystem aDefault = FileSystems.getDefault(); System.out.println(aDefault.getClass()); FileSystemProvider provider = FileSystems.getDefault().provider(); System.out.println(provider.getClass()); System.out.println("====================" + uri.getScheme()); List<FileSystemProvider> fileSystemProviders = FileSystemProvider.installedProviders(); fileSystemProviders.forEach(p -> { System.out.println(p.getClass()); }); Path path = Paths.get(uri); }
class sun.nio.fs.WindowsFileSystemclass sun.nio.fs.WindowsFileSystemProvider====================fileclass sun.nio.fs.WindowsFileSystemProviderclass com.sun.nio.zipfs.ZipFileSystemProvider
public static Path get(URI uri) { String scheme = uri.getScheme(); if (scheme == null) throw new IllegalArgumentException("Missing scheme");
// check for default provider to avoid loading of installed providers if (scheme.equalsIgnoreCase("file")) return FileSystems.getDefault().provider().getPath(uri);
// try to find provider for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { if (provider.getScheme().equalsIgnoreCase(scheme)) { return provider.getPath(uri); } }
throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed"); }
Path path = null; try{ path = Paths.get(uri); }catch (Exception e){ //@see https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p //@see http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html FileSystem zipfs = FileSystems.newFileSystem(uri, env); path = Paths.get(uri); }
或者使用其他办法加载资源文件:
byte[] data;try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) { data = IOUtils.toByteArray(in);}