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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to solve the file reading problem in the SpringBoot jar package", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to solve the file reading problem in the SpringBoot jar package"!
Preface
SpringBoot micro-service has become the mainstream of the industry, from development to deployment are very time-saving and labor-saving, but recently Xiaoming encountered a problem: read resource files in the code (such as word documents, export templates, etc.), local development can be read normally, however, when we pack the jar package and release to the server, when we execute the program again, we will throw an exception that the file cannot be found.
Background
This problem was discovered when a word report was exported using the freemarker template engine. Roughly talk about docx export java implementation ideas: export word document format for docx, prepare a prepared version of the docx document as a template, read and parse the template, replace the static resources in it and then export.
The docx document itself is actually a compressed zip file, and you'll find that it has its own directory structure after unzipping it.
problem
The directory of this docx document is shown in the following figure:
When debugging locally, I read as follows:
Import org.springframework.util.ResourceUtils
Public static void main (String [] args) throws IOException {
File docxTemplate = ResourceUtils.getFile ("classpath:templates/docxTemplate.docx")
}
It can be parsed and used normally, but packaging and publishing to the beta environment is not available. An exception is thrown as follows:
Java.io.FileNotFoundException: class path resource [templates/docxTemplate.docx] cannot be resolved to absolute file path because it does not reside in the file system: jarVera fileDiscue usruse localAccording to match match server.jarbank, Bootcom INFhand, classeslect, template.docx
Obviously, this exception tells us that the file was not found, but after unzipping the jar package, we found that the file was real. So what's going on here? It doesn't bother me at all. We should be good at looking at the essence through stack information. By looking closely at the stack information, I found that the file path at this time is not a legal URL (file resource locator). It turns out that the resources in the jar package have their own special URL form: jar _ entry _ resources / {jar}). Therefore, if it is still in the form of standard file resource location at this time,
File f=new File ("jar:file: …")
Locate the file and a java.io.FileNotFoundException will be thrown.
Solve
Although we cannot read the resource file docxTemplate.docx in the jar package using the usual file manipulation method, we can get it through the getResourceAsStream () method of the Class class, that is, through the stream:
Public static void main (String [] args) throws IOException {
InputStream inputStream = WordUtil.class.getClassLoader () .getResourceAsStream ("templates/docxTemplate.docx")
}
After getting the stream, we can convert it to any object we need, such as File, String, and so on. Here I want to get the directory structure under docxTemplate.docx, so I need a File object. The code example is as follows:
Import org.apache.commons.io.FileUtils
Public static void main (String [] args) throws IOException {
InputStream inputStream = WordUtil.class.getClassLoader () .getResourceAsStream ("templates/docxTemplate.docx")
File docxFile = new File ("docxTemplate.docx")
/ / can be converted using the utility class of common-io
FileUtils.copyToFile (inputStream,docxFile)
ZipFile zipFile = new ZipFile (docxFile)
Enumeration
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.