1. 程式人生 > 實用技巧 >springboot專案圖片上傳,回顯;使用外部靜態資源路徑回顯圖片

springboot專案圖片上傳,回顯;使用外部靜態資源路徑回顯圖片

//前端圖片是Base64字串形式傳遞圖片引數;需要用Base解密,寫入到本地磁碟中
public String upload(String string){
 
    解析圖片(Base64):
    response.setHeader("Access-Control-Allow-Origin","*");  // 第二個引數填寫允許跨域的域名稱,不建議直接寫 "*"
    response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
    response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
 
    
// 接收跨域的cookie response.setHeader("Access-Control-Allow-Credentials", "true"); url = url.substring(url.indexOf(",")+1,url.length()); String imagePath = null; BASE64Decoder decoder = new BASE64Decoder(); String strName = null; String string2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); System.out.println(string2);
try { byte[] bytes = decoder.decodeBuffer(url); // 處理資料 for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { bytes[i] += 256; } } strName = UUID.randomUUID().toString(); System.out.println(strName); imagePath
= "E:/javaworkspace/project/rbApi/image/"+strName+".png"; OutputStream out = new FileOutputStream(imagePath); out.write(bytes); out.flush(); out.close(); }catch (Exception e){ e.printStackTrace(); } } //springboot專案 //以@RequestParam("url") List<MultipartFile> url接收上傳圖片;寫入到本地磁碟中 //返回路徑是磁碟路徑,將磁碟路徑對映到外部靜態資源;訪問:專案路徑+對映路徑+檔名 @RequestMapping(value = "/uploadImage") @ResponseBody public PictureUrl upload(@RequestParam("url") List<MultipartFile> url, HttpServletRequest request, HttpServletResponse response){ PictureUrl pictureUrl = new PictureUrl(); String contentPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); try{ if(url.size() == 0){ pictureUrl.setStatusText("false"); }else{ OutputStream os = null; List<String> urlList = new ArrayList<>(); for (MultipartFile file : url){ String fileName = file.getOriginalFilename(); String fileNam2 = UUID.randomUUID().toString(); String imagePath = "E:/javaworkspace/project/rbApi/image/"+fileNam2+".png"; File file1 = new File(imagePath); os = new FileOutputStream(file1); os.write(file.getBytes()); //String imagePath2 = contentPath + "/showImage?id=" + fileNam2+".png"; //String imagePath2 = upload + fileNam2+".png"; String imagePath2 = contentPath+"/image/"+fileNam2+".png"; urlList.add(imagePath2); System.out.println("檔案路徑:"+imagePath2); } pictureUrl.setStatusText("ok"); pictureUrl.setImageUrl(urlList); os.flush(); os.close(); } }catch (Exception e){ e.printStackTrace(); pictureUrl.setStatusText("false"); return pictureUrl; } return pictureUrl; } //將圖片以二進位制的形式輸出,前端路徑可為:(專案路徑+方法+檔名) /** * 回顯圖片 * @param id * @param response * @return */ @RequestMapping("/showImage") @ResponseBody public AjaxResult download(String id, HttpServletResponse response){ try{ response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg"); String path ="E:/javaworkspace/project/rbApi/image/"+id; File file = new File(path);//括號裡引數為檔案圖片路徑 if(file.exists()){ //如果檔案存在 InputStream in = new FileInputStream(path); //用該檔案建立一個輸入流 OutputStream os = response.getOutputStream(); //建立輸出流 byte[] b = new byte[1024]; while( in.read(b)!= -1){ os.write(b); } in.close(); os.flush(); os.close(); } return null; }catch (Exception e){ e.printStackTrace(); return AjaxResult.build("false",null ); } } //將磁碟檔案路徑對映為專案訪問路徑 @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/image/**").addResourceLocations("file:E:/javaworkspace/project/rbApi/image/"); } }