這里我將自己學習的項目為例子作個簡單的記錄:
在html圖片的路徑如圖:
這里是頭像路徑的映射
然后要映射到阿里雲Linux服務器上路徑:
注意,這兩個路徑是不同的,只是同名而已,HTML那里的路徑可以隨便修改,到最后映射到這個路徑就可以,當然映射到別的路徑也可以
映射方法:
找到tomcat下的config下的server.xml文件
在Host節點加上下面的:
前面是path是虛擬路徑,對應的是HTML那里的代碼,后面是真實路徑,對應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("執行完了"); } return dataGridView; }
順便說下war包放到阿里雲服務器上路徑映射(域名或者IP直接訪問項目根路徑):
<Context path="/" docBase="/home/tomcat/apache-tomcat-8.5.45/webapps/code007" debug="0" reloadable="true"/>