spring boot自帶圖片服務(wù)器使用詳解
我們平時(shí)在日常項(xiàng)目中經(jīng)常會(huì)遇到圖片的上傳和訪問(wèn)的情景,平時(shí)我們可能習(xí)慣于把圖片傳到resource或者項(xiàng)項(xiàng)目中的某個(gè)位置,這樣會(huì)有一個(gè)缺點(diǎn),當(dāng)我們重新項(xiàng)目打包時(shí),這些圖片會(huì)丟失。為了解決這一缺點(diǎn),我們只有把圖片的路徑放到項(xiàng)目外,而springboot集成了映射項(xiàng)目外路徑的這一功能。ps:當(dāng)然目前一些大的項(xiàng)目,會(huì)有多個(gè)子系統(tǒng)都用到文件上傳和下載,這時(shí)搭建文件服務(wù)器是最好的選擇。
上傳的實(shí)現(xiàn)請(qǐng)看:Spring Boot實(shí)現(xiàn)圖片上傳功能 這位大神在里面講的很詳細(xì);
下面請(qǐng)看springboot如何訪問(wèn)項(xiàng)目外的圖片:
首先要寫個(gè)配置類:
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(這里用一句話描述這個(gè)類的作用)
* @author Administrator
* @date 2017年7月11日
*/
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
//獲取配置文件中圖片的路徑
@Value("${cbs.imagesPath}")
private String mImagesPath;
//訪問(wèn)圖片方法
@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);
}
}
注意:如果項(xiàng)目中有攔截器,一定要添加不要攔截圖片路徑,方法如下:
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/getLogin")
.excludePathPatterns("/api/getExit");
super.addInterceptors(registry);
}
這樣啟動(dòng)項(xiàng)目就可以獲取路徑下的圖片了:訪問(wèn)地址例如:localhost:8080/images/123.png
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法
- SpringBoot上傳文件到本服務(wù)器 目錄與jar包同級(jí)問(wèn)題
- springboot打包部署到linux服務(wù)器的方法
- Spring boot項(xiàng)目部署到云服務(wù)器小白教程詳解
- SpringBoot war包部署到Tomcat服務(wù)器
- spring Boot打包部署到遠(yuǎn)程服務(wù)器的tomcat中
- bootstrap-table實(shí)現(xiàn)服務(wù)器分頁(yè)的示例 (spring 后臺(tái))
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
- Spring Boot + thymeleaf 實(shí)現(xiàn)文件上傳下載功能
- SpringBoot實(shí)現(xiàn)文件上傳下載功能小結(jié)
- springboot實(shí)現(xiàn)文件上傳和下載功能
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問(wèn)題
相關(guān)文章
數(shù)據(jù)庫(kù)連接池c3p0配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了數(shù)據(jù)庫(kù)連接池c3p0配置的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
SpringCloudGateway?Nacos?GitlabRunner全自動(dòng)灰度服務(wù)搭建發(fā)布
這篇文章主要為大家介紹了SpringCloudGateway?Nacos?GitlabRunner全自動(dòng)灰度服務(wù)搭建和發(fā)布實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Spring導(dǎo)入properties配置文件代碼示例
這篇文章主要介紹了Spring導(dǎo)入properties配置文件代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
SpringBoot設(shè)置接口超時(shí)時(shí)間的方法
這篇文章主要介紹了SpringBoot設(shè)置接口超時(shí)時(shí)間的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
基于IDEA查看maven依賴結(jié)構(gòu)流程解析
這篇文章主要介紹了基于IDEA查看maven依賴結(jié)構(gòu)流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
springBoot之如何獲取接口請(qǐng)求數(shù)據(jù)和返回?cái)?shù)據(jù)實(shí)現(xiàn)日志
這篇文章主要介紹了springBoot之如何獲取接口請(qǐng)求數(shù)據(jù)和返回?cái)?shù)據(jù)實(shí)現(xiàn)日志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

