文件在idea中运行时不报错,打包运行后报错java.lang.IllegalArgumentException: URI is not hierarchical

这个问题造成的原因是放在类路径classpath中的文件,打包后文件找不到所造成的。
这种方式在打包之前可以创建file文件,但是打包成jar包后,会由于路径改变后在template/LZGICAD1.mdb找不到该文件而报错URI is not hierarchical。

File file =  new File("template/LZGICAD1.mdb");

在这里插入图片描述
解决方案:

//创建一个随即路径
File tempFolder = FileUtil
		.mkdir(tmpDirPath + "\\TempMDB\\" 
		+ System.currentTimeMillis() + "_" + (int) (Math.random() * 10000));

ClassPathResource classPathResource =  new ClassPathResource("template/LZGICAD1.mdb");
//根据类路径获取resource下面文件的流
InputStream inputStream = classPathResource.getInputStream();
//在本地生成一个文件
File file = new File(tempFolder, "LZGICAD1.mdb");
//通过类路径中的文件流来覆盖本地随机生成的文件
File mdbModel = FileUtil.writeFromStream(inputStream, file,true);

通过 new ClassPathResource(“template/LZGICAD1.mdb”)获取类路径,打包后不会改变,再通过 classPathResource.getInputStream()获取类路径文件的流。

本地创建一个文件,FileUtil.writeFromStream(inputStream, file,true) 来覆盖本地文件。

猜你喜欢

转载自blog.csdn.net/weixin_44860226/article/details/131956857