如何使用springboot中自带的图片服务器

今天就跟大家聊聊有关如何使用spring boot中自带的图片服务器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的十载时间我们累计服务了上千家以及全国政企客户,如三轮搅拌车等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致夸奖。

首先要写个配置类:

application.properties文件中的路径配置如下

cbs.imagesPath=file:/E:/imagesuuuu/

配置类如下:

package bp.config;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @ClassName: WebAppConfig
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Administrator
 * @date 2017年7月11日
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
   //获取配置文件中图片的路径
 @Value("${cbs.imagesPath}")
 private String mImagesPath;
 //访问图片方法
 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
  if(mImagesPath.equals("") || mImagesPath.equals("${cbs.imagesPath}")){
   String imagesPath = WebAppConfig.class.getClassLoader().getResource("").getPath();
   if(imagesPath.indexOf(".jar")>0){
    imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
   }else if(imagesPath.indexOf("classes")>0){
    imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes"));
   }
   imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/images/";
   mImagesPath = imagesPath;
  }
  LoggerFactory.getLogger(WebAppConfig.class).info("imagesPath="+mImagesPath);
  registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath);
  super.addResourceHandlers(registry);
 }
}

注意:如果项目中有拦截器,一定要添加不要拦截图片路径,方法如下:

@Override
 public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/getLogin")
    .excludePathPatterns("/api/getExit");
  super.addInterceptors(registry);

 }

 这样启动项目就可以获取路径下的图片了:访问地址例如:localhost:8080/images/123.png

看完上述内容,你们对如何使用spring boot中自带的图片服务器有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

本文标题:如何使用springboot中自带的图片服务器
文章起源:https://www.cdcxhl.com/article6/jdijog.html

成都网站建设公司_创新互联,为您提供营销型网站建设企业建站网页设计公司网站策划软件开发面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作