Spring 如何在用户主目录中提供静态资源
我在~/YYT/ProfilePicture文件夹中有一些图片。我想让SpringBoot将这些图片用作静态资源。请求url类似于“”。我尝试在application.properties文件中编写Spring 如何在用户主目录中提供静态资源,spring,spring-boot,Spring,Spring Boot,我在~/YYT/ProfilePicture文件夹中有一些图片。我想让SpringBoot将这些图片用作静态资源。请求url类似于“”。我尝试在application.properties文件中编写spring.resources.static locations=file:~/YYT/property。我还尝试过使用webmvcconfiguer。它们都不起作用 @Configuration public class WebStaticResourceConfig implements Web
spring.resources.static locations=file:~/YYT/
property。我还尝试过使用webmvcconfiguer
。它们都不起作用
@Configuration
public class WebStaticResourceConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String homeDirectory = "file:" + Paths.get(System.getProperty("user.home"), "YYT/ProfilePicture").toString();
System.out.println(homeDirectory);
registry.addResourceHandler("/ProfilePicture/**").addResourceLocations(homeDirectory);
}
}
路径应更改如下:
spring.resources.static-locations=file:/YYT/
您的
主目录
丢失或缺少一个尾随/
使用path.toUri()
生成有效的uri,然后使用它
Path Path=Path.get(System.getProperty(“user.home”),“YYT/ProfilePicture”);
字符串homeditory=path.toUri().toString();
registry.addResourceHandler(“/ProfilePicture/**”).addResourceLocations(homeDirectory);
现在,homeditory
字符串应该是正确的