欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用SpringBoot設(shè)置虛擬路徑映射絕對路徑

 更新時間:2021年08月20日 10:37:24   作者:mdw5521  
這篇文章主要介紹了使用SpringBoot設(shè)置虛擬路徑映射絕對路徑的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot 設(shè)置虛擬路徑映射絕對路徑

上傳圖片到本地路徑,得到的是一個絕對路徑例如:D:\picpath\O48681516429132485.png

但是前臺需要的數(shù)據(jù)是這樣的 :http://localhost:8082/image/O48681516429132485.png

那么就要設(shè)置虛擬路徑 /image/ = D:\picpath\ 了,

下面我們就來代碼實(shí)現(xiàn)下

作為一個負(fù)責(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í)的項(xiàng)目為例子作個簡單的記錄:

在html圖片的路徑如圖

這里是頭像路徑的映射

然后要映射到阿里云Linux服務(wù)器上路徑

注意,這兩個路徑是不同的,只是同名而已,HTML那里的路徑可以隨便修改,到最后映射到這個路徑就可以,當(dāng)然映射到別的路徑也可以

映射方法

找到tomcat下的config下的server.xml文件

在Host節(jié)點(diǎn)加上下面的

前面是path是虛擬路徑,對應(yīng)的是HTML那里的代碼,后面是真實(shí)路徑,對應(yīng)Linux上面真實(shí)路徑

這里順便放上后臺接收上傳頭像的代碼

@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直接訪問項(xiàng)目根路徑):

<Context path="/" docBase="/home/tomcat/apache-tomcat-8.5.45/webapps/code007" debug="0" reloadable="true"/>

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論