Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在用户主目录中提供静态资源_Spring_Spring Boot - Fatal编程技术网

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

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

我在~/YYT/ProfilePicture文件夹中有一些图片。我想让SpringBoot将这些图片用作静态资源。请求url类似于“”。我尝试在application.properties文件中编写
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
字符串应该是正确的