使用SpringBoot設(shè)置虛擬路徑映射絕對路徑
SpringBoot 設(shè)置虛擬路徑映射絕對路徑
上傳圖片到本地路徑,得到的是一個絕對路徑例如:D:\picpath\O48681516429132485.png
但是前臺需要的數(shù)據(jù)是這樣的 :http://localhost:8082/image/O48681516429132485.png
那么就要設(shè)置虛擬路徑 /image/ = D:\picpath\ 了,
下面我們就來代碼實現(xiàn)下
作為一個負責(zé)任的程序員,我把包也給你們復(fù)制過來了。
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * 圖片絕對地址與虛擬地址映射 */ @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //文件磁盤圖片url 映射 //配置server虛擬路徑,handler為前臺訪問的目錄,locations為files相對應(yīng)的本地路徑 registry.addResourceHandler("/image/**").addResourceLocations("D:\\picpath\\"); } }
是不是很簡單呢?
springboot打war包圖片的虛擬路徑映射
這里我將自己學(xué)習(xí)的項目為例子作個簡單的記錄:
在html圖片的路徑如圖
這里是頭像路徑的映射
然后要映射到阿里云Linux服務(wù)器上路徑
注意,這兩個路徑是不同的,只是同名而已,HTML那里的路徑可以隨便修改,到最后映射到這個路徑就可以,當(dāng)然映射到別的路徑也可以
映射方法
找到tomcat下的config下的server.xml文件
在Host節(jié)點加上下面的
前面是path是虛擬路徑,對應(yīng)的是HTML那里的代碼,后面是真實路徑,對應(yīng)Linux上面真實路徑
這里順便放上后臺接收上傳頭像的代碼
@ResponseBody @RequestMapping("uploadImage") public DataGridView uploadImage(MultipartFile file, HttpSession session) throws Exception { DataGridView dataGridView = null; if (!file.isEmpty()){ String filename = file.getOriginalFilename(); //abc.jpg String suffix = filename.substring(filename.lastIndexOf(".")); //后綴 如abc.jpg,就是jpg String newFileName = DateUtil.getCurrentDateStr() + suffix; //新文件名 FileUtils.copyInputStreamToFile(file.getInputStream(),new File(userImageFilePath+newFileName)); Map<String,Object> map= new HashMap<>(); map.put("src","/project/userImages/"+newFileName); map.put("title",newFileName); dataGridView = new DataGridView(0, "上傳成功", map); User currentUser = (User) session.getAttribute("currentUser"); currentUser.setImageName(newFileName); userService.save(currentUser); session.setAttribute("currentUser",currentUser); System.out.println("執(zhí)行完了"); } return dataGridView; }
順便說下war包放到阿里云服務(wù)器上路徑映射(域名或者IP直接訪問項目根路徑):
<Context path="/" docBase="/home/tomcat/apache-tomcat-8.5.45/webapps/code007" debug="0" reloadable="true"/>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑
這篇文章主要介紹了Java中使用正則表達式獲取網(wǎng)頁中所有圖片的路徑,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06解決springboot與springcloud版本兼容問題(附版本兼容表)
在基于spring boot搭建spring cloud時,創(chuàng)建eureka后啟動服務(wù)發(fā)生報錯,本文給大家介紹了解決springboot與springcloud版本兼容問題的幾種方案,需要的朋友可以參考下2024-02-02spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03使用@JsonFormat和@DateTimeFormat對Date格式化操作
這篇文章主要介紹了使用@JsonFormat和@DateTimeFormat對Date格式化操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08springboot前后臺數(shù)據(jù)交互的示例代碼
這篇文章主要介紹了springboot前后臺數(shù)據(jù)交互的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10spring整合redis以及使用RedisTemplate的方法
本篇文章主要介紹了spring整合redis以及使用RedisTemplate的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05