NIO文件系统异常分析与解决方法
在使用nio加载文件时,在idea中运行没有问题,但打成jar包后在windows和linux下都有问题:代码语言:javascript代码运行次数:0运行复制publicstaticvoidmain(String[]args)throwsException{//SpringApplication.run(MarketCollectApplication.class,args);URIuri=MarketCollectApplication.cla
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 fileSystemProviders = FileSystemProvider.installedProviders(); fileSystemProviders.forEach(p -> { System.out.println(p.getClass()); }); Path path = Paths.get(uri); } 这种情况下在idea中没有问题:代码语言:javascript代码运行次数:0运行复制class sun.nio.fs.WindowsFileSystemclass sun.nio.fs.WindowsFileSystemProvider====================fileclass sun.nio.fs.WindowsFileSystemProviderclass com.sun.nio.zipfs.ZipFileSystemProvider但是在打成jar包运行时Path path = Paths.get(uri)这一行会抛出异常:windows环境下: 

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"); }uri.getScheme()在idea中是file,在打成jar包后变成了jar。当前缀以file开头时,会使用FileSystems.getDefault().provider()来处理,这个provider在windows环境下是WindowsFileSystemProvider, 在linux环境下是LinuxFileSystemProvider。FileSystemProvider.installedProviders()对应windows中的WindowsFileSystemProvider和ZipFileSystemProvider,对应linux中的LinuxFileSystemProvider和ZipFileSystemProvider。当前缀不以file开头时,会使用FileSystemProvider.installedProviders()中与uri.getScheme()匹配的provider来处理,对应的就是ZipFileSystemProvider。ZipFileSystemProvider对应的FileSystem需要自己创建,使用和创建方式参考:https://docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/zipfilesystemprovider.html解决办法:在Path path = Paths.get(uri)中进行处理代码语言:javascript代码运行次数:0运行复制 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); }或者使用其他办法加载资源文件:
代码语言:javascript代码运行次数:0运行复制byte[] data;try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) { data = IOUtils.toByteArray(in);}参考:https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p
Postman是一套面向API全生命周期的开发与测试平台,可在Mac上发送和调试多种协议请求,管理集合、环境及认证信息,并完成脚本测试、模拟服务、监控、文档发布和团队协作。它适合开发、测试及运维人员使用,提供个人免费方案。
Docker Desktop 是 Docker 官方为 Mac 提供的容器开发环境,集成 Docker Engine、命令行工具、Compose、Build、Kubernetes 与图形化管理界面。
Android Studio 是 Google 面向安卓应用开发提供的官方集成开发环境,基于 IntelliJ IDEA 构建。它整合智能代码编辑、Gradle 构建、界面预览、设备模拟、性能分析、调试测试及 APK 分析等工具。
Postman 是面向 API 全生命周期的开发与协作平台。Windows 桌面版可用于构建和发送 HTTP、GraphQL、gRPC、WebSocket、MQTT 等请求,并提供环境变量、集合、脚本测试、模拟服务、监控、文档及团队工作区等
Docker Desktop 为 Windows 开发者提供集成式容器环境,内置 Docker Engine、命令行工具、Compose、Build 与 Kubernetes 等组件,可在本机完成镜像构建、容器运行、日志查看和资源管理。
Android Studio 是 Google 面向 Android 应用开发提供的官方集成开发环境,以 IntelliJ IDEA 为基础,集成智能代码编辑、Gradle 构建、设备模拟、界面预览、调试测试及性能分析工具,









