author :liuxiaopeng
https://www.cnblogs.com/paddix/p/8301331.html
do web When developing , We tend to have a lot of static resources , Such as html、 picture 、css etc. . So how to return static resources to the front end ?
I've done it before web Development students should know , What we created before web There will be a webapp The catalog of , We only need to put static resources in this directory to directly access .
however , be based on Spring boot The project does not have this catalog , What should we do ?
One 、 The stupidest way
Let's first share the stupidest way , That is, static resources are directly returned to the front end through the flow , We are maven engineering resources To create a html The catalog of , And then we put html The files are placed in this directory , And specify any access path to /static/ The first one is to access the static resources under the directory , It is realized as follows :
@Controllerpublic class StaticResourceController { @RequestMapping("/static/**") public void getHtml(HttpServletRequest request, HttpServletResponse response) { String uri = request.getRequestURI(); String[] arr = uri.split("static/"); String resourceName = "index.html"; if (arr.length > 1) { resourceName = arr[1]; } String url = StaticResourceController.class.getResource("/").getPath() + "html/" + resourceName; try { FileReader reader = new FileReader(new File(url)); BufferedReader br = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); line = br.readLine(); } response.getOutputStream().write(sb.toString().getBytes()); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } }}
In fact, the implementation process is very simple , Is to separate resources from the path first uri, And then from static Read files in directory , And output to the front end .
Because it's just a simple demonstration , So we only deal with text files , Image files can be processed in a similar way . Of course , We're not going to do that in practice ,Spring Boot There must be a better solution .
But this method is a little stupid , But it's the most essential thing , No matter how convenient the framework helps us deal with such problems , But get rid of the framework , We still need to be able to write a web project , Only know how it works , You'll be able to handle problems when they come up .
Now let's see Spring boot Support for static resources .
Two 、Spring boot Default static resource access
Spring boot Default pair /** Can directly access files in four directories :
classpath:/public/
classpath:/resources/
classpath:/static/
classpath:/META-INFO/resouces/
We are now in the resource file resources Create the following four directories under the directory :
Note the resource folder under the blue bar resources And the folder under the classpath classpath:/resources Is different , Under the blue bar resources Represents that the files in this directory are resource files , When packing, all files in this directory will be packed in the classpath , This name can be changed , stay pom.
<resources> <resource> <.........