Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read resources catalog files by SpringBoot

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "SpringBoot how to read resources directory files", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "SpringBoot how to read resources directory files" bar!

In the Java coding process, we often want to read the configuration files within the project, according to Maven custom, these files are generally placed under the project src/main/resources, therefore, the contract agreement PDF template, Excel format statistical reports and other templates are stored in resources/template/test.pdf, the following provides two read methods, they are in windows and Linux environment (linux jar package) can run normally.

Method one: ClassPathResource

String pdfFilePath = "template/test.pdf"; Resource resource = new ClassPathResource (pdfFilePath)

You can convert Resource to InputStream by using the following methods:

InputStream is = resource.getInputStream ()

Method 2 getContextClassLoader

InputStream inputStream = Thread.currentThread () .getContextClassLoader () .getResourceAsStream (pdfFilePath)

Test case

Public static void main (String [] args) {try {String pdfFilePath = "template/test.pdf"; Resource resource = new ClassPathResource (pdfFilePath); System.out.println (resource.getURI () + "--* path ="); if (resource.isReadable ()) {/ / opens a new stream InputStream is = resource.getInputStream () every time; System.out.println (method one + is.available ()) } InputStream inputStream = Thread.currentThread (). GetContextClassLoader (). GetResourceAsStream (pdfFilePath); System.out.println ("method II" + inputStream.available ());} catch (IOException e) {e.printStackTrace ();}}

Thank you for your reading, the above is the content of "SpringBoot how to read resources directory files". After the study of this article, I believe you have a deeper understanding of how SpringBoot reads resources directory files, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report