About the inability to read the static resources of the project root path after the SpringBoot project is started through the jar package

Foreword: This was discovered last night when a project was deployed, record it here

About the inability to read the static resources of the project root path after the SpringBoot project is started through the jar package

Problem Description

After deploying a project, I opened the project page for testing, and found that a query page failed to query and automatically jumped to the error page. I looked at the log and found that a file read failed, as shown below:

error message

insert image description here

The directory where the file to be read is located

insert image description here

The code for the file reading part

    public String readFileContent() {
    
    
        File file = new File("src/main/resources/static/ticket/station_name.txt");
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();

        try {
    
    
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
    
    
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (reader != null) {
    
    
                try {
    
    
                    reader.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

At that time, I was quite confused, because this file can still be read during local testing, why can't it be found on the server? ? ?

Solution

I checked the Internet and found that there is a problem with using File to obtain local resources in the server.

Reason:
The files in the Resources directory exist in the file xxx.jar, and there is no real path on the server disk. It is actually a path inside the jar.

Solution 1 (stupid method):
Upload the file to be read to the corresponding directory of the server, and change the read file path to the path corresponding to the file on the server.

Solution 2:
Use the input stream, as follows:

    public String readFileContent() throws Exception{
    
    

        String filePath = "static/ticket/station_name.txt";
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();

        //文件读取
        inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String tempStr;
        while ((tempStr = reader.readLine()) != null) {
    
    
            sbf.append(tempStr);
        }
        reader.close();
        return sbf.toString();
    }
    public String readFileContent() throws Exception{
    
    

        String filePath = "static/ticket/station_name.txt";
        ClassPathResource resource = new ClassPathResource(filePath);
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();

        //文件读取
        inputStream = resource.getInputStream();
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String tempStr;
        while ((tempStr = reader.readLine()) != null) {
    
    
            sbf.append(tempStr);
        }
        reader.close();
        return sbf.toString();
    }

It should be noted here that the path used is the path under resources.

PS: You can also go to my personal blog to see more content
Personal blog address: Xiaoguan classmate's blog

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/121450856