Springboot gets the files in the resources resource directory in the jar package

Assassin multi-problem phenomenon:

        Today, I encountered a business scenario in the project and needed to use the files in the resources resource directory , and then I was thinking about a question:

        When the project is packaged into a jar, how does Springboot obtain the files in the resources resource directory?


problem analysis:

        As in the title, how can I still get the files in the resources directory of the jar package in the code after the project is packaged into a jar package?

        We all know that there are many ways to obtain the files under the resources resource directory; although these methods can be effective when running the project locally with normal debug, but not all of them are effective when the project is packaged and run as a jar package. ! ! !

        Therefore, here I intend to record a pro-test and feasible method.

        First, let me show my project structure, as shown in the figure, I created a folder rules under the resources resource directory, and then created 3 drl files in it:

        Since the code logic is very simple, it is tested directly by starting the class here.

        ResourceDemoApplication:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.File;
import java.io.IOException;

/**
 * resource资源目录专题
 * @author Stephen
 */
@SpringBootApplication
public class ResourceDemoApplication {

	public static void main(String[] args) {
//		SpringApplication.run(ResourceDemoApplication.class, args);
		try {
			//如何获取resource下的文件?【注意:即使打成jar包也得有效】
			ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
			Resource[] resources = resourcePatternResolver.getResources("classpath*:rules/**/*.*");
			for ( Resource resource : resources ) {
				//获取文件名
				System.out.println(resource.getFilename());

				//获取文件,该方式在打成jar包后会报错,因为resource文件不在系统中,而是在jar包中
				File file1 = resource.getFile();

				//获取文件,在打成jar包后,通过url来获取文件,则路径是正确的
				File file = new File(resource.getURL().getFile());

				//获取文件绝对路径,但路径有问题,因为resource文件不在系统中,而是在jar包中
//				System.out.println(file.getAbsolutePath());

				//获取文件名
				System.out.println(file.getName());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

        The following shows the running results.

        First the local project:        

        As you can see, the file name is printed out very smoothly.

        Then the jar package runs:

        Also no problem.


Notice:

        It should be noted that in  the ResourceDemoApplication startup class, there are two lines of code that are commented out:

        The above line of code is fine to run locally, but the following error will appear when running as a jar package, so it is not recommended to use:

        The displayed file path is:

jar:file:/C:/Users/Administrator/Desktop/resource_demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/rules/comparison-rules.drl

        You can see that  resource_demo-0.0.1-SNAPSHOT.jar is followed by a ! Then it is the path in the target package of the corresponding drl file. I have not delved into the details, but if I analyze it only from this violent file path, my The understanding is that this file path is actually a false path, it is not the real url address of the file, so the file cannot be obtained through the resource object.

        

        Another line of commented code is:

         Execute this line of code, even when the jar package is running, no error will be reported, but when the jar package is running, the absolute path of the printed file is not the real path, that is, the system cannot recognize it, because this file is inside the jar package, the path as follows:

         If there is an extension, it will be added later.


Solution:

		try {
			ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
			Resource[] resources = resourcePatternResolver.getResources("classpath*:rules/**/*.*");
			for ( Resource resource : resources ) {
				//获取文件,在打成jar包后,通过url来获取文件,则路径是正确的
				File file = new File(resource.getURL().getFile());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/130348018