Remember once an exception FileNotFoundException

  Colleagues submitted code, go back to the Spring Festival, today, business people yesterday for him to change things there are problems, I want to deal with. I looked under the log and found that this is very easy exception

java.io.FileNotFoundException: \excel-templates\123 (系统找不到指定的路径。)

  See the error message, you know xls template is not on the server to get the program, looked is there, it is transported Wei Gege precipice of a pot on the next git.

  Lifting decisive dimension to (Projects under this docker environment, developers can not see the code on the server, no authority)

  Operation and maintenance under investigation, found that the server is xls template, again question threw me.

Error submitting project template file

  Odd strange, there are templates on the server, git also have to submit records, Why is this so?

  With doubts, I tried the local, find, also will complain, fileNotFoundException, my heart suddenly MMP, which too pulled it, that man is not measured, submitted go up?

  Carefully read his code, found that he is a local test (because the local test code also submitted go up the ...), however, is the way he measured the absolute path of the disk, such as "c: // temp / abc .xls ", this way is not a problem.

  However, the actual use of the code is to use relative paths to get the classpath, for example: "classpath: excel-templates / abc.xls", we project a father and son project, he only submitted xls template in the parent project, not in the common project in submission template.

  Locating the problem, upload the template, in the common local test, everything is normal.

  ok, submit code, the restart line.

ResourceUtils.getFile () did not get the expected value

  I thought all right, result, self-test, I found, is still being given the wrong information is still fileNotFoundException

  Under the server to read, common following templates exist

  OMG, shocked, a little cynicism

  You can only launch a trick - Valley Oriented Programming song

  However, there is no gain to valuable information

  The project code, very simple line of code, use the spring to the util class

public static FileInputStream getTemplates(String tempName) throws FileNotFoundException {
        return new FileInputStream(ResourceUtils.getFile("classpath:excel-templates/"+tempName));
}

Quxianjiuguo - Replace get template program

  In desperation, can only Quxianjiuguo - way to get a new one. Use getResourceAsStream way, the result is really normal ...

  Attach the following codes:

public static InputStream getTemplates(String tempName) {
    InputStream resourceAsStream = TemplateFileUtil.class.getResourceAsStream("/excel-templates/" + tempName);
    return resourceAsStream;
}

What both get template file differences are?

  Grilled have to go this deep source code, I too hard ---

Published 158 original articles · won praise 193 · Views 1.48 million +

Guess you like

Origin blog.csdn.net/zy_281870667/article/details/104054245