spring boot自带图片服务器使用详解

作者:众星捧月 时间:2021-11-07 07:49:39 

我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失。为了解决这一缺点,我们只有把图片的路径放到项目外,而springboot集成了映射项目外路径的这一功能。ps:当然目前一些大的项目,会有多个子系统都用到文件上传和下载,这时搭建文件服务器是最好的选择。

上传的实现请看:Spring Boot实现图片上传功能 这位大神在里面讲的很详细;

下面请看springboot如何访问项目外的图片:

首先要写个配置类:

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

来源:http://www.cnblogs.com/bestxyl/p/7403297.html

标签:spring,boot,图片服务器
0
投稿

猜你喜欢

  • mybatis中<if>标签bool值类型为false判断方法

    2023-11-20 11:28:33
  • Spring Boot 在启动时进行配置文件加解密的方法详解

    2023-11-12 17:13:54
  • 面试官:详细谈谈Java对象的4种引用方式

    2022-09-19 04:52:20
  • java.lang.Runtime.exec的左膀右臂:流输入和流读取详解

    2023-08-06 04:59:03
  • java中String的一些方法深入解析

    2023-11-25 21:48:56
  • Java 守护线程_动力节点Java学院整理

    2023-11-28 07:51:14
  • Java中List.of()和Arrays.asList()的区别及原因分析

    2023-08-06 00:33:16
  • Java并发的CAS原理与ABA问题的讲解

    2023-11-25 12:17:21
  • C++实现LeetCode(205.同构字符串)

    2023-06-21 04:06:54
  • springboot+gradle 构建多模块项目的步骤

    2023-02-19 00:33:31
  • JAVA 实现磁盘文件加解密操作的示例代码

    2023-11-15 00:13:06
  • Java金额大小写的转换方法

    2023-08-23 00:26:11
  • java 读取网页内容的实例详解

    2023-11-27 22:38:11
  • SpringMVC一步到位精通拦截器

    2023-11-25 01:47:45
  • 详解java IO流之缓冲流的使用

    2023-08-08 18:33:16
  • 详解Eclipse 字体、字号的设置、最佳字体推荐

    2023-11-26 12:25:32
  • 详解Swagger接口文档和常用注解的使用

    2023-11-24 15:35:21
  • Java创建子线程的两种方法

    2023-11-24 07:00:05
  • Java import导入及访问控制权限修饰符原理解析

    2023-08-17 21:42:59
  • Servlet3.0学习总结之基于Servlet3.0的文件上传实例

    2023-09-02 01:51:33
  • asp之家 软件编程 m.aspxhome.com