SpringBoot - 访问静态资源
原创
©著作权归作者所有:来自51CTO博客作者wx62eb7326068f3的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.从classpath/static访问
在resources下建立一个static的folder(必须是static名字的),放一张图片,用来做测试
相当简单,执行这个main就OK了
package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
访问:
http://localhost:8080/1.png
图片粗来,OK
然后改版一下,将图片放到imgs文件夹下,写一个index.html,访问这个html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>访问静态资源</title>
</head>
<body>
静态资源:
<img alt="photo" src="imgs/1.png">
</body>
</html>
访问:http://localhost:8080/index.html
发现没有乱码,图片也粗来了(使用SpringBoot感觉很爽的一点是,它基本上都是基于UTF-8的)
2.访问servletContext下
在src/main下创建webapp文件夹(必须是这个名字的)
index可以使用原理的index.html
然后访问http://localhost:8080/index2.html
图片和文字都粗来了,OK