In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "springboot reading files into jar package access can not be solved" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, hope that this "springboot read files into jar packages after access can not solve" article can help you solve the problem.
Springboot reads files, which cannot be accessed after being packed into jar packets.
There is a situation in the latest development that the file cannot be read after springboot is packaged into jar package, because after packaging, the virtual path of the file is invalid and can only be read through stream.
Files under resources public void test () {List names = new ArrayList (); InputStreamReader read = null; try {ClassPathResource resource = new ClassPathResource ("name.txt"); InputStream inputStream = resource.getInputStream (); read = new InputStreamReader (inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader (read); String txt = null; while ((txt = bufferedReader.readLine ())! = null) {if (StringUtils.isNotBlank (txt)) {names.add (txt) } catch (Exception e) {e.printStackTrace ();} finally {if (read! = null) {try {read.close ();} catch (IOException e) {e.printStackTrace ();} springboot cannot access the static folder 1.ResourceUtils with jar package background
Usually, when we write spring boot projects, we occasionally use the files under classpath in the background. This is what we usually write.
File file = ResourceUtils.getFile ("classpath:static/image/image")
There would have been no problem under the circumstances. But after running it with a jar package, you will not be able to find this file.
The file under Resource exists in the file jar. There is no real path on disk. It is actually a path inside jar. So you cannot get the file correctly through the ResourceUtils.getFile or this.getClass (). GetResource ("") method.
For this kind of situation. Sometimes project documents are placed outside the project, but it is easy to delete these things by mistake.
2.ClassPathResource ClassPathResource cpr = new ClassPathResource ("static/image/image/kpg"); InputStream in = cpr.getInputStream (); 3. ResourceLoader public class ResourceRenderer {public static InputStream resourceLoader (String fileFullPath) throws IOException {ResourceLoader resourceLoader = new DefaultResourceLoader (); return resourceLoader.getResource (fileFullPath). GetInputStream ();}}
Usage
InputStream in = ResourceRenderer.resourceLoader ("classpath:static/image/image")
This perfectly solves the problem that the path under the jar package is inaccessible.
This is the end of the content about "how to solve the problem when springboot reads files into jar packages". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.