In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
SpringBoot how to get the files under the src/main/resource path, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
SpringBoot gets the file under the src/main/resource path
The following ResourceUtils uses spring-core 's toolkit, under the org.springframework.util package
File file = ResourceUtils.getFile (ResourceUtils.CLASSPATH_URL_PREFIX+ "static/imgs/aha.png")
However, if you type the Boot project into a jar package, the file will not be available and the following path will not be found:
/ EVOGRAGUP testUniverse demoMile 1.1.0.Jarleman BOOTRANINFAccording to staticsUniverse Aha.png
Because the jar package is no longer stored in a disk folder, you can read the stream of the file in jar as follows if you want to package it into a jar package, and then transfer the stream to your own needs (war can also be used in this way)
InputStream inputStream = this.getClass () .getResourceAsStream ("/ static/imgs/aha.png"); after SpringBoot is packed into a jar package, the general method for reading files in the resources directory is Properties pps = new Properties (); File file = ResourceUtils.getFile ("classpath:defult.properties"); pps.load (new FileReader (file))
At this point, the runtime packaged as jar will report an error:
Java.io.FileNotFoundException
During debugging, a file is a directory that actually exists on disk. At this point, it can be read normally by getting the file path, because the file does exist.
After being packaged into jar, the file is actually a resource file that exists in the jar file, and there is no real path on disk. So you cannot get the file correctly through the ResourceUtils.getFile or this.getClass (). GetResource ("") method.
Correct method
It is processed in the way of stream, and the encoding utf-8 is set when the stream is read.
Using InputStream inputStream=this.getClass (). GetResourceAsStream (") specifies that the path of the resource to be loaded is the same as the path of the package in which the current class resides. Therefore, the file can be read normally.
Properties pps = new Properties (); InputStream stream = getClass () .getClassLoader () .getResourceAsStream ("defult.properties"); BufferedReader br = new BufferedReader (new InputStreamReader (stream, "UTF-8")); pps.load (br); is it helpful for you to finish reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.