No error is reported when the file is run in idea, but java.lang.IllegalArgumentException: URI is not hierarchical is reported after packaging and running

The reason for this problem is that the files placed in the classpath of the classpath cannot be found after packaging.
This method can create a file file before packaging, but after packaging into a jar package, the error URI is not hierarchical will be reported because the file cannot be found in template/LZGICAD1.mdb after the path is changed.

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

insert image description here
solution:

//创建一个随即路径
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);

Obtain the class path through new ClassPathResource("template/LZGICAD1.mdb"), which will not change after packaging, and then obtain the stream of the class path file through classPathResource.getInputStream().

Create a file locally, FileUtil.writeFromStream(inputStream, file,true) to overwrite the local file.

Guess you like

Origin blog.csdn.net/weixin_44860226/article/details/131956857