spring-boot配置外部静态资源的方法
import java.io.File; import javax.servlet.Servlet; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@Order(Ordered.HIGHEST_PRECEDENCE )
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
@Slf4j
public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*
File directory = new File("");
String path2=directory.getAbsolutePath();
StringBuilder builder = new StringBuilder();
builder.append("file:").append(path2).append("/m/"); //registry.addResourceHandler("/**").addResourceLocations( builder.toString() );
//String myExternalFilePath = "file:///C:/Temp/whatever/m/";
log.info( builder.toString() );
*/ registry.addResourceHandler("/m/**").addResourceLocations("file:m/");
//registry.addResourceHandler("/m/**").addResourceLocations(builder.toString()); super.addResourceHandlers(registry);
}
}
spring-boot配置外部静态资源的方法的更多相关文章
- spring boot 配置虚拟静态资源文件
我们实现的目的是:通过spring boot 配置静态资源访问的虚拟路径,可实现在服务器,或者在本地通过:http://ip地址:端口/资源路径/文件名 ,可直接访问文件 比如:我们本地电脑的:E: ...
- Spring Boot实战:静态资源处理
前两章我们分享了Spring boot对Restful 的支持,不过Restful的接口通常仅仅返回数据.而做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静 ...
- Spring Boot 中的静态资源到底要放在哪里?
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...
- Spring Boot中的静态资源文件
Spring Boot中的静态资源文件 1.SSM中的配置 2.Spring Boot 中的配置 2.1 整体规划 2.2 源码解读 2.3 自定义配置 2.3.1 application.prope ...
- tomcat配置外部静态资源映射路径
一.背景 1.有一个录音软件每天生成很多新的录音文件. 2.现在想通过一个WEB项目页面下载这些录音文件. 3.很显然这些录音文件放在WEB项目下不是很合适(WEB项目更新是个大麻烦,海量的录音文件要 ...
- 【spring boot】7.静态资源和拦截器处理 以及继承WebMvcConfigurerAdapter类进行更多自定义配置
开头是鸡蛋,后面全靠编!!! ======================================================== 1.默认静态资源映射路径以及优先顺序 Spring B ...
- spring boot 2.x静态资源会被HandlerInterceptor拦截的原因和解决方法
在spring boot 1.5.x中,resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,比如静态资源放置位置如下图所示: 那么访问静态资源的路径可以是: ...
- spring boot 2.0 + 静态资源被拦截,怎么办?
问题描述:使用springboot 2.0后,按照springboot 1.5版本(以下简称旧版)的方式去配置项目.结果发现静态资源访问不到了,本文对此情况分析.处理 项目结构: 直接上图 如果是在旧 ...
- (4)Spring Boot Web开发---静态资源
文章目录 对静态资源的映射规则 模板引擎 Thymeleaf 使用 & 语法 使用之前将的快速创建项目的方法,勾选我们需要的场景,这里我需要 web --> web.sql --> ...
随机推荐
- 详解HTTP中的摘要认证机制(转)
Basic认证方式是存在很多缺陷的,具体表现如下: 1, Basic认证会通过网络发送用户名和密码,并且是以base64的方式对用户名和密码进行简单的编码后发送的,而base64编码本身非常容易被解 ...
- HDU 5754 Life Winner Bo (博弈)
Life Winner Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5754 Description Bo is a "Life W ...
- Gradle – Spring 4 MVC Hello World Example
In this tutorial, we will show you a Gradle + Spring 4 MVC, Hello World Example (JSP view), XML conf ...
- 如何解决paramiko执行与否的问题
使用paramiko执行一些耗时比较长的命令的时候会出现实际上命令没有执行完就跳出的问题,怎么才能准确的判断命令执行完与否很重要,通过试验发现如下的方法可以解决这个难题: dabao_cmd = 'e ...
- HDU 5835 Danganronpa (水题)
题意:给定 n 个礼物有数量,一种是特殊的,一种是不特殊的,要分给一些人,每人一个特殊的一个不特殊,但是不特殊的不能相邻的,问最多能分给多少人. 析:是一个比较简单的题目,我们只要求差值就好,先算第一 ...
- ASP导出Word带页眉页脚,中文不乱码
关键代码: <% Response.Clear() Response.CodePage= Response.Charset="UTF-8" Response.ContentT ...
- C++中标准容器Vector,元素操作.insert()小结
insert() 函数有以下三种用法: iterator insert( iterator loc, const TYPE &val ); //在指定位置loc前插入值为val的元素,返回指 ...
- applicationContext.xml存放的位置
web.xml中classpath:和classpath*: 有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...
- Hadoop 2.4.0全然分布式平台搭建、配置、安装
一:系统安装与配置 虚拟机软件:Virtualbox 4.3.10(已安装对应扩展包) 虚拟机:Ubuntu 13.04 LTS 32位(至于为什么选择13.04,是由于最新的版本号装上后开机会出现错 ...
- AlertView with password
1. setAlertViewStyle:UIAlertViewStyleSecureTextInput UIAlertView *alertView = [[UIAlertView alloc] i ...