The way to get the relative path of a file under the resources folder in the maven project

The way to get the relative path of a file under the resources folder in the maven project

		ClassPathResource classPathResource = new ClassPathResource("templates/" + "/导出洪水要素表模板.xlsx");
        InputStream is = classPathResource.getInputStream();

The ClassPathResource class is under the springframework-core package

Generally speaking, the configuration files and static resources of our project will be placed in the resources directory. Sometimes we use the files in the resources directory in the project, then we can use the Resource interface under Spring to read them.
Because Resource is an interface, we can use its implementation class ClassPathResource to new an object. The parameter of the construction method is the file path in the resources directory. Note that this is the relative path used (relative to the resources directory).
After we get the resource object, we can call the resource.getFile() method to get the file

Guess you like

Origin blog.csdn.net/weixin_44756075/article/details/117016998