1 /* 2 白名單請求都直接放行: 3 */ 4 List<String> urlList = new ArrayList<>(); 8 //對上傳圖片的url介面/product/img-upload的請求直接放行 9 urlList.add("/product/img-upload"); 10 //對static下的/img/upload中的靜態資源圖片的訪問直接放行 11 if(urlList.contains(path)||path.contains("/img/upload")){ 12 chain.doFilter(request, response); 13 return; 14 }
1 /** 2 * 上傳圖片的url介面/product/img-upload 3 * 4 * 引數MultipartFile file物件封裝了上傳的圖片; 5 * 6 * @CrossOrigin表示該url介面允許跨域請求; 7 */ 8 @CrossOrigin 9 @PostMapping("/img-upload") 10 public Result uploadImage(MultipartFile file){ 11 12 try { 13 //拿到圖片上傳到的目錄(類路徑classes下的static/img/upload)的File物件 14 File uploadDirFile = ResourceUtils.getFile(uploadPath); 15 //拿到圖片上傳到的目錄的磁碟路徑 16 String uploadDirPath = uploadDirFile.getAbsolutePath(); 17 //拿到圖片儲存到的磁碟路徑 18 String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename(); 19 //儲存圖片 20 file.transferTo(new File(fileUploadPath)); 21 return Result.ok("圖片上傳成功!"); 22 } catch (Exception e) { 23 return Result.err(Result.CODE_ERR_BUSINESS,"圖片上傳失敗!"); 24 } 25 }
1 #application.properties檔案 2 # 圖片上傳位置 3 file.upload-path=classpath:static/img/upload 4 # 上傳的圖片儲存資料庫的訪問路徑 5 file.access-path=/img/upload/