SpringBoot配置圖片訪問的虛擬路徑
記錄一次SpringBoot配置虛擬路徑訪問圖片的筆記
最近編寫的項(xiàng)目都是需要將圖片進(jìn)行訪問的,而我的是有spring+springMVC+Mybatis框架實(shí)現(xiàn)的項(xiàng)目,并且在使用ssm框架的時(shí)候已經(jīng)是用到了圖片訪問的虛擬路徑來進(jìn)行訪問的,ssm配置虛擬路徑實(shí)在Tomcat上配置的圖片訪問,而SpringBoot是內(nèi)置Tomcat的那應(yīng)該怎么配置呢具體看下圖,
先配置圖片上傳路徑
這個(gè)是jsp頁面的代碼段
<div class="layui-form-item"> <label class="layui-form-label">簡介圖片</label> <div class="layui-upload layui-input-block"> <button type="button" class="layui-btn" id="SingleUpload"> <i class="layui-icon layui-icon-upload"></i> 上傳圖片 </button> <img id="simpleImg" width="60px" height="60px"> </div> </div>
js代碼段
upload.render({ //這里是上傳一張圖片 elem: "#SingleUpload", url: ctx + "/book/SingleUpload", done: function (res, index, upload) { //假設(shè)code=0代表上傳成功 if (res.code == 0) { layer.msg("簡介圖片加載成功!", {icon: 1}); $("#simpleImg").attr("src", res.image); $("#SingleUpload").addClass("layui-btn-disabled"); $("#SingleUpload").off("click"); } } });
接下來是Controller里面的具體配置
private String simplePath = "D:/uploadLibrary/"; // 詳細(xì)圖片地址 private StringBuilder detailsPath = new StringBuilder(); @RequestMapping("/SingleUpload") @ResponseBody public Map<String, Object> SingleUpload(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpSession session) { Map<String, Object> map = new HashMap<String, Object>(); try { String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); String filename = UUID.randomUUID() + suffixName; File filePath = new File(dirPath); if (!filePath.exists()) { filePath.mkdirs(); } //創(chuàng)建虛擬路徑存儲(chǔ) simplePath = req.getServletContext().getContextPath() + "/file/" + filename; // simplePath = filename; map.put("image", simplePath); file.transferTo(new File(dirPath + filename)); map.put("code", 0); map.put("msg", "上傳成功"); } catch (Exception e) { map.put("code", 1); map.put("msg", "上傳失敗"); e.printStackTrace(); } return map; }
數(shù)據(jù)庫存儲(chǔ)的圖片路徑
一切都設(shè)置好了過后這時(shí)就需要對(duì)SpringBoot配置虛擬路徑來對(duì)圖片進(jìn)行訪問了
新建config 控制類在里面新建類方法WebMvcConfig來對(duì)圖片進(jìn)行虛擬路徑的配置
具體代碼
package com.book.libratyman.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/file/**").addResourceLocations("file:D:/uploadLibrary/"); } }
addResourceHandler("/file/**")是我在項(xiàng)目中訪問圖片的路徑也就是數(shù)據(jù)里面的圖片存儲(chǔ)路徑,而addResourceLocations(“file:D:/uploadLibrary/”)則是我上傳圖片的真實(shí)路徑我上傳圖片的真實(shí)路徑是 **D:/uploadLibrary/**配置以后運(yùn)行項(xiàng)目便可以訪問項(xiàng)目圖片了。
圖片顯示出來就表示已經(jīng)配置成功了哦?。。。。?/p>
到此這篇關(guān)于SpringBoot配置圖片訪問的虛擬路徑的文章就介紹到這了,更多相關(guān)SpringBoot配置圖片訪問內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)戰(zhàn)之XML與JavaConfig的混合配置詳解
大家都知道Spring的顯示配置方式有兩種,一種是基于XML配置,一種是基于JavaConfig的方式配置。那么下這篇文章主要給大家分別介紹如何在JavaConfig中引用XML配置的bean以及如何在XML配置中引用JavaConfig,需要的朋友可以參考下。2017-07-07Spring boot通過AOP防止API重復(fù)請(qǐng)求代碼實(shí)例
這篇文章主要介紹了Spring boot通過AOP防止API重復(fù)請(qǐng)求代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java concurrency線程池之線程池原理(三)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理第三篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法
這篇文章主要介紹了關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07