如何在用户主目录中提供静态资源

时间:2019-02-22 07:01:47

标签: spring spring-boot

我在〜/ YYT / ProfilePicture文件夹中有一些图片。我想让Spring Boot将这些图片用作静态资源。请求网址类似于“ {http://localhost:8080/ProfilePicture/4/takeovertheworld.jpg”。我尝试在application.properties文件中编写SELECT REGEXP_REPLACE('PSU 12.1.0.2.170117', '^.*?([0-9.]+).*$', '\1') FROM dual; 属性。我也尝试使用spring.resources.static-locations=file:~/YYT/。他们都不起作用。

WebMvcConfigurer

2 个答案:

答案 0 :(得分:1)

您丢失的homeDirectory缺少结尾的/

使用path.toUri()生成有效的uri,然后再使用它。

Path path = Paths.get(System.getProperty("user.home"), "YYT/ProfilePicture");
String homeDirectory = path.toUri().toString();
registry.addResourceHandler("/ProfilePicture/**").addResourceLocations(homeDirectory);

现在homeDirectory字符串应该正确。

答案 1 :(得分:0)

路径应如下更改:

spring.resources.static-locations=file:/YYT/
相关问题