在用spring boot框架进行开发时,有时我们需要取到上传到项目中的图片来进行展示
在application中进行配置需要获取项目下哪个文件下的东西
jc.css.path=classpath:/static/css/
jc.imgs.path=classpath:/static/imgs/
jc.js.path=classpath:/static/js/
jc.static.path=classpath:/static/
jc.imguploads.path=classpath:/static/imguploads/
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.EnableWebMvc;
importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public classMyWebAppConfigurerextendsWebMvcConfigurerAdapter
{
//取出在application中配置的要取出的路径
@Value("${jc.tmp.path}")
privateStringlocalImgPath;
@Value("${jc.css.path}")
privateStringlocalCssPath;
@Value("${jc.js.path}")
privateStringlocalJsPath;
@Value("${jc.imgs.path}")
privateStringlocalImgsPath;
@Value("${jc.static.path}")
privateStringlocalStaticPath;
@Value("${jc.imguploads.path}")
privateStringlocalImguploadsPath;
@Override
public voidaddResourceHandlers(ResourceHandlerRegistry registry)
{
String path ="file:"+localImgPath;
//需要项目路径+请求名字+需要访问的具体的文件的名字
registry.addResourceHandler("/imgupload/**").addResourceLocations(path);
registry.addResourceHandler("/css/**").addResourceLocations(localCssPath);
registry.addResourceHandler("/js/**").addResourceLocations(localJsPath);
registry.addResourceHandler("/imgs/**").addResourceLocations(localImgsPath);
registry.addResourceHandler("/static/**").addResourceLocations(localStaticPath);
registry.addResourceHandler("/imguploads/**").addResourceLocations(localImguploadsPath);
super.addResourceHandlers(registry);
}
}
这样我们就可以取出我们需要的文件了
网友评论