spring boot自带图片服务器使用详解
作者:众星捧月 发布时间:2021-11-07 07:49:39
标签:spring,boot,图片服务器
我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到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


猜你喜欢
- 本文实例讲述了Android实现Service获取当前位置(GPS+基站)的方法。分享给大家供大家参考。具体如下:需求详情:1)、Servi
- 本文为大家分享了Android中Drawable方法的详细使用方法,供大家参考,具体内容如下1. BitmapDrawable相关方法:新建
- 文件上传是开发中十分常见的功能,在servlet3.0之前,实现文件上传需要使用一些插件技术,比如:commons-fileuploadsm
- 依赖的jar包:spring相关jar包版本:4.3.1.RELEASE<dependency> &l
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- int、String的类型转换int -> Stringint i=12345;String s="";第一种方法
- 在Android开发中,有时我们需要对一个对象的集合按照某一个字段进行排序,Beanpublic class Student { priva
- 什么是Swagger?Swagger是什么:THE WORLD'S MOST POPULAR API TOOLING根据官网的介绍:
- 数据库事务是后端开发中不可缺少的一块知识点。Spring为了更好的支撑我们进行数据库操作,在框架中支持了两种事务管理的方式: 编程式事务声明
- 1.由于springboot集成了tomcat,所以打包的时候不再使用war,而是使用jar <groupId>cn&
- 本文通过介绍了Asp.Net中MVC缓存的种类,以及他们之间的区别等内容,让学习者能够深入的了解MVC缓存的原理机制,以下是具体内容:缓存是
- 返回值类型:System.Int64表示为 Windows 文件时间的当前 DateTime 对象的值。例如:DateTime.Now.To
- 引言: Spring Boot号称微服务的利器,在结合了Spring Data与JPA之后,更是如虎添翼,开发快速的不像话,本文将讲述一个关
- 隐藏标题栏基于xml<application android:theme="@style/Them
- java 枚举的功能挺多,但是坑更多,使用的时候要注意。如下面这个枚举。@Getter@AllArgsConstructorpublic e
- 一.以springboot为例,建立代码1.IExecCommandServer:public interface IExecCommand
- 图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的“Windows照片查看器”中的上一张、下一张切换图片的功能
- 在Java中,泛型的引入是为了在编译时提供强类型检查和支持泛型编程。为了实现泛型,Java编译器应用类型擦除实现: &
- 1.官方地址:http://mybatis.plus/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%
- 本文实例为大家分享了Unity实现全屏截图、Unity实现QQ截图,供大家参考,具体内容如下全屏截图:要实现的是点击鼠标左键,就实现截图,并