1、默认静态资源映射

  Spring Boot对静态资源映射提供了默认配置

  Spring Boot默认将 /** 所有访问映射到一下目录

    classpath:/static

    classpath:/public

    classpath:/resources

    classpath:/META-INF/resources

  

   在resources目录下新建 META-INF/resources、public、resources、static 四个目录,并分别放入 4.jpg 3.jpg 2.jpg  1.jpg图片

  

  启动项目,在浏览器分别输入

     http://localhost:8080/4.jpg

     http://localhost:8080/3.jpg

     http://localhost:8080/2.jpg

     http://localhost:8080/1.jpg

例如:

2、自定义静态资源文件访问

  (1)、第一种方式:配置类

    

  将所有C:\\Users\\Administrator\\Pictures\\Fairy\\访问都映射到/myPic/**路径下

     重启项目后,例如在C:\Users\Administrator\Pictures\Fairy中有一张1.jpg图片,

     在浏览器输入http://localhost:8080/myPic/1.jpg即可访问

  (2)、配置application.properties

    web.upload-path=C:\Users\Administrator\Pictures\Fairy

    spring.mvc.static-path-pattern=/**

    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

  web.upload-path:这个属于自定义的属性,指定了一个路径,注意要以/结尾;

  spring.mvc.static-path-pattern=/**:表示所有的访问都经过静态资源路径;

  spring.resources.static-locations:在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上否则static、public等这些路径将不能被当作静态资源路径,在这个最末尾的file:${web.upload-path}之所有要加file:是因为指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量。

重启项目,例如在C:\Users\Administrator\Pictures\Fairy下有一张lion.jpg

  在浏览器地址中输入http://localhost:8080/lion.jpg即可

SpringBoot静态资源文件的更多相关文章

  1. springboot读取静态资源文件的方式

    springboot的请求路径一般会经过Controller处理,但是静态资源文件在请求之后是直接返回的.这涉及到俩个配置项. spring.mvc.static-path-pattern=/** s ...

  2. springboot 静态资源访问,和文件上传 ,以及路径问题

    springboot 静态资源访问: 这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...

  3. 解决springboot 配置文件未映射静态资源文件 导致shiro拦截静态资源的问题

    ---------------------------------------------------------------------------------------------------- ...

  4. SpringBoot读取静态资源文件

    ClassPathResource resource = new ClassPathResource(publicKeyCer); File file = null; try { file = res ...

  5. SpringMVC+FreeMarker实现静态资源文件自动添加版本号(md5)

    近日切换java开发,开始学习springframework.在实现静态资源文件自动计算版本号的实例时,因为不熟悉框架,走了不少弯路,好在最终解决了问题.这里写篇文章记录一下实现,也希望对大家有些用处 ...

  6. Springboot解决资源文件404,503等特殊报错,无法访问

    Springboot解决资源文件404,503等特殊报错 原文链接:https://www.cnblogs.com/blog5277/p/9324609.html 原文作者:博客园--曲高终和寡 ** ...

  7. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...

  8. springboot静态资源映射

    springboot静态资源映射 WebMvcAutoConfiguration @Override public void addResourceHandlers(ResourceHandlerRe ...

  9. springmvc、springboot静态资源访问配置

    如何访问项目中的静态资源? 一.springmvc springmvc中访问静态资源,如果DispatcherServlet拦截的为"",那么静态资源的访问也会交给Dispatch ...

随机推荐

  1. 3-11 group操作拓展

    In [1]: import pandas as pd import numpy as np df=pd.DataFrame({'A':['foo','bar','foo','bar', 'foo', ...

  2. Nginx内置变量以及日志格式变量

    $args #请求中的参数值$query_string #同 $args$arg_NAME #GET请求中NAME的值$is_args #如果请求中有参数,值为"?",否则为空字符 ...

  3. 解决IIS7、IIS7.5 应用程序池回收假死的方法

    最近iis网站一直假死状态,都懵了,查看程序有没有关闭数据库,反复捣鼓,还一直测试是否是程序应用池自动回收问题依然没有效果.经过老师提醒,找到了解决办法,在此做个笔记! 原因在于:应用程序池超时配置被 ...

  4. Bandit

    CSE599:online and adaptive machine learning Lecture 3:Stochastic Multi-Armed Bandits, Regret Minimiz ...

  5. Educational Codeforces Round 57 (Rated for Div. 2) D dp

    https://codeforces.com/contest/1096/problem/D 题意 给一个串s,删掉一个字符的代价为a[i],问使得s的子串不含"hard"的最小代价 ...

  6. 【CF1097F】Alex and a TV Show

    [CF1097F]Alex and a TV Show 题面 洛谷 题解 我们对于某个集合中的每个\(i\),令\(f(i)\)表示\(i\)作为约数出现次数的奇偶性. 因为只要因为奇偶性只有\(0, ...

  7. [LeetCode] 139. Word Break 拆分词句

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  8. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  9. ORACLE--10G安装问题( error while loading shared libraries)

    01,问题描述 问题一: WARNING: directory '/u01/app/oracle/product/10.2.0' is not owned by root WARNING: direc ...

  10. RabbitMQ心跳检测与掉线重连

    1.RabbitMQListener,自定义消息监听器 using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using ...