


How to solve the problem that springboot cannot access the file after reading it into a jar package
springboot reads the file, but cannot access it after packaging it into a jar package
In the latest development, there is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be read through the stream.
The file is under resources
public void test() { List<string> 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(); } } } }</string>
springboot cannot access the static folder in the jar package background
1.ResourceUtils
Usually when we write spring boot projects Occasionally, files under the classpath are used in the background. Generally, we write them like this
File file = ResourceUtils.getFile("classpath:static/image/image");
In this case, there is no problem. But after running the jar package, the file will not be found.
The file under Resource exists in the jar file. There is no real path on the disk. It is actually a path inside the jar. Therefore, the file cannot be obtained correctly through the ResourceUtils.getFile or this.getClass().getResource("") method.
In this case. 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 of inaccessible paths under the jar package.
The above is the detailed content of How to solve the problem that springboot cannot access the file after reading it into a jar package. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.