springboot取出上传的图片方法

在用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);

}

}

这样我们就可以取出我们需要的文件了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容