SpringBoot静态资源的访问和配置(或者说文件上传配置)

首先科普下涉及的API:

String dir = System.getProperty("user.dir");//获得项目当前路径
/*
与系统有关的默认名称分隔符。此字段被初始化为包含系统属性 file.separator 值的第一个字符。
在 UNIX 系统上,此字段的值为 '/';在 Microsoft Windows 系统上,它为 '\'。
*/
File.separatorChar;

/*
与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
此字符串只包含一个字符,即 separatorChar。
*/
File.separator;

/*
与系统有关的路径分隔符。此字段被初始为包含系统属性 path.separator 值的第一个字符。
此字符用于分隔以路径列表 形式给定的文件序列中的文件名。在 UNIX 系统上,
此字段为 ':';在 Microsoft Windows 系统上,它为 ';'。
*/
File.pathSeparatorChar;

/*
与系统有关的路径分隔符,为了方便,它被表示为一个字符串。此字符串只包含一个字符,
即 pathSeparatorChar
*/
File.pathSeparator;

其实windows下也能识别“/”,但不保证以后能不能,所有最好还是用默认的

正题
我为什么要配置静态资源的访问:为了上传图片到SpringBoot项目,由于是jar包方式运行的,所以没有外置tomcat,不能像以前那样在外置tomcat的webapps目录新建一个upload文件夹然后把文件存到upload,所以需要配置。

配置有两种方式:
方法一:通过配置文件配置
修改application.yml或application.properties

spring:
    mvc:
        static-path-pattern: /**
    resources:
        static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, file:D:/upload

默认不修改前application文件是这样的:

spring:
    mvc:
        static-path-pattern: /**
    resources:
         static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/

static-path-pattern是什么到后面一起解释

方法二:通过@Configuration配置

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		String dir = System.getProperty("user.dir");

		//System.out.println("项目当前路径:"+dir);
		//构建路径
		File file=new File(dir+File.separatorChar+"ImageData");
		if(!file.exists()){
			file.mkdir();
		}
		String resourceLocation=file.getAbsolutePath()+File.separatorChar;
		//System.out.println(resourceLocation+">>>>>>");

		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/META-INF/resources/")
				.addResourceLocations("classpath:/resources/")
				.addResourceLocations("classpath:/static/")
				.addResourceLocations("classpath:/public/")
				.addResourceLocations("file:"+resourceLocation);
		super.addResourceHandlers(registry);
	}
}

这里的addResourceHandler和static-path-pattern作用是一样的,可以理解为映射 触发匹配表达式(手动断好句了),如果把/** 换成 /my/ **,那么浏览器访问xxx/my/**时后端就会根据映射规则去classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/和 “file:”+resourceLocation里面找对应的静态文件,如果是/ **,则会覆盖默认SpringMVC的配置(其实就是classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/,如果要改的话加上这一大串再加上自己要设置的就没事了)。

推荐使用第二种,能适应不同的系统,存储路径也是动态的。

需要注意的几点:

  1. resourceLocation最后一个字符一定是一个名称分隔符(’/‘或者’’),表示目录
  2. 再新版本的SpringBoot中WebMvcConfigurerAdapter不推荐使用,推荐使用WebMvcConfigurationSupport ,但WebMvcConfigurationSupport 有些小区别,具体区别请看博文:https://www.cnblogs.com/deng720/p/8989388.html,
    其实也就是一旦我们继承了WebMvcConfigurationSupport,SpringBoot默认的配置就失效了(如/ ** 映射到classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/的配置就不存在了,需要全部重写一遍)

推荐博文:
https://yq.aliyun.com/articles/552747
https://blog.csdn.net/yiifaa/article/details/78299052
https://blog.csdn.net/biren_wang/article/details/78947558
https://blog.csdn.net/u011144425/article/details/79225864

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值