SpringBoot配置圖片訪問的虛擬路徑
記錄一次SpringBoot配置虛擬路徑訪問圖片的筆記
最近編寫的項目都是需要將圖片進行訪問的,而我的是有spring+springMVC+Mybatis框架實現(xiàn)的項目,并且在使用ssm框架的時候已經是用到了圖片訪問的虛擬路徑來進行訪問的,ssm配置虛擬路徑實在Tomcat上配置的圖片訪問,而SpringBoot是內置Tomcat的那應該怎么配置呢具體看下圖,
先配置圖片上傳路徑
這個是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) {
//假設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/";
// 詳細圖片地址
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)建虛擬路徑存儲
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ù)庫存儲的圖片路徑

一切都設置好了過后這時就需要對SpringBoot配置虛擬路徑來對圖片進行訪問了
新建config 控制類在里面新建類方法WebMvcConfig來對圖片進行虛擬路徑的配置
具體代碼
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/**")是我在項目中訪問圖片的路徑也就是數(shù)據(jù)里面的圖片存儲路徑,而addResourceLocations(“file:D:/uploadLibrary/”)則是我上傳圖片的真實路徑我上傳圖片的真實路徑是 **D:/uploadLibrary/**配置以后運行項目便可以訪問項目圖片了。


圖片顯示出來就表示已經配置成功了哦?。。。?!
到此這篇關于SpringBoot配置圖片訪問的虛擬路徑的文章就介紹到這了,更多相關SpringBoot配置圖片訪問內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring實戰(zhàn)之XML與JavaConfig的混合配置詳解
大家都知道Spring的顯示配置方式有兩種,一種是基于XML配置,一種是基于JavaConfig的方式配置。那么下這篇文章主要給大家分別介紹如何在JavaConfig中引用XML配置的bean以及如何在XML配置中引用JavaConfig,需要的朋友可以參考下。2017-07-07
Java concurrency線程池之線程池原理(三)_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency線程池之線程池原理第三篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

