我在〜/ YYT / ProfilePicture文件夹中有一些图片。我想让Spring Boot将这些图片用作静态资源。请求网址就像“ http:// localhost:8080 / ProfilePicture / 4 / takeovertheworld.jpg ”。我尝试spring.resources.static-locations=file:~/YYT/
在application.properties文件中写入属性。我也尝试使用WebMvcConfigurer
。他们都不工作。
@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);
}
}
您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
字符串应该是正确的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句