spring boot自帶圖片服務器使用詳解
我們平時在日常項目中經(jīng)常會遇到圖片的上傳和訪問的情景,平時我們可能習慣于把圖片傳到resource或者項項目中的某個位置,這樣會有一個缺點,當我們重新項目打包時,這些圖片會丟失。為了解決這一缺點,我們只有把圖片的路徑放到項目外,而springboot集成了映射項目外路徑的這一功能。ps:當然目前一些大的項目,會有多個子系統(tǒng)都用到文件上傳和下載,這時搭建文件服務器是最好的選擇。
上傳的實現(xiàn)請看:Spring Boot實現(xiàn)圖片上傳功能 這位大神在里面講的很詳細;
下面請看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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- spring boot2.0圖片上傳至本地或服務器并配置虛擬路徑的方法
- SpringBoot上傳文件到本服務器 目錄與jar包同級問題
- springboot打包部署到linux服務器的方法
- Spring boot項目部署到云服務器小白教程詳解
- SpringBoot war包部署到Tomcat服務器
- spring Boot打包部署到遠程服務器的tomcat中
- bootstrap-table實現(xiàn)服務器分頁的示例 (spring 后臺)
- 詳解SpringBoot下文件上傳與下載的實現(xiàn)
- Spring Boot + thymeleaf 實現(xiàn)文件上傳下載功能
- SpringBoot實現(xiàn)文件上傳下載功能小結
- springboot實現(xiàn)文件上傳和下載功能
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- spring boot搭建文件服務器解決同時上傳多個圖片和下載的問題
相關文章
SpringBoot使用CORS實現(xiàn)無縫跨域的方法實現(xiàn)
CORS 是一種在服務端設置響應頭部信息的機制,允許特定的源進行跨域訪問,本文主要介紹了SpringBoot使用CORS實現(xiàn)無縫跨域的方法實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-10-10SpringSecurity學習之自定義過濾器的實現(xiàn)代碼
這篇文章主要介紹了SpringSecurity學習之自定義過濾器的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01Java如何將int型數(shù)組轉為String型數(shù)組
這篇文章主要介紹了Java如何將int型數(shù)組轉為String型數(shù)組,本文給大家分享具體實現(xiàn)思路結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-03-03java遠程連接Linux執(zhí)行命令的3種方式完整代碼
在一些Java應用程序中需要執(zhí)行一些Linux系統(tǒng)命令,例如服務器資源查看、文件操作等,這篇文章主要給大家介紹了關于java遠程連接Linux執(zhí)行命令的3種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-06-06