vue+springboot图片上传和显示的示例代码(2)

@RequestMapping(value = "/imageUpload", method = RequestMethod.POST) public void imageUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) { try { logger.info("上传图片名 :" + file.getOriginalFilename()); if (!file.isEmpty()) { // Properties p = new Properties();// 属性集合对象 // String path = RedisUtil.class.getClassLoader().getResource("").getPath()+"global.properties"; // FileInputStream fis = new FileInputStream(path);// 属性文件输入流 // p.load(fis);// 将属性文件流装载到Properties对象中 // fis.close();// 关闭流 // String uploadPath = p.getProperty("imgUpload.url"); // //路径名称上加上-年/月日:yyyy/MMdd // uploadPath += "Uploads/"+new SimpleDateFormat("yyyy").format(new Date())+ "https://www.jb51.net/" +new SimpleDateFormat("MMdd").format(new Date())+"https://www.jb51.net/"; String path= request.getServletContext().getRealPath("https://www.jb51.net/"); path="/Users/qinshengfei/fsdownload"; logger.error("path:"+path); //路径名称上加上-年/月日:yyyy/MMdd String uploadPath = File.separatorChar+"Uploads"+File.separatorChar+new SimpleDateFormat("yyyy").format(new Date())+ File.separatorChar +new SimpleDateFormat("MMdd").format(new Date())+File.separatorChar; // 文件上传大小 long fileSize = 10 * 1024 * 1024; //判断文件大小是否超过 if (file.getSize() > fileSize) { backInfo(response, false, 2, ""); return; } //获取上传文件名称 String OriginalFilename = file.getOriginalFilename(); //获取文件后缀名:如jpg String fileSuffix = OriginalFilename.substring(OriginalFilename.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.asList(TypeMap.get("image").split(",")).contains(fileSuffix)) { backInfo(response, false, 3, ""); return; } //判断是否有文件上传 // if (!ServletFileUpload.isMultipartContent(request)) { // backInfo(response, false, -1, ""); // return; // } // 检查上传文件的目录 File uploadDir = new File(path+uploadPath); System.out.println(path+uploadPath); if (!uploadDir.isDirectory()) { if (!uploadDir.mkdirs()) { backInfo(response, false, 4, ""); return; } } // 是否有上传的权限 if (!uploadDir.canWrite()) { backInfo(response, false, 5, ""); return; } // 新文件名-加13为随机字符串 String newname = getRandomString(13) +"." + fileSuffix; File saveFile = new File(path+uploadPath, newname); try { file.transferTo(saveFile); backInfo(response, true, 0, uploadPath+newname); } catch (Exception e) { logger.error(e.getMessage(), e); backInfo(response, false, 1, ""); return; } } else { backInfo(response, false, -1, ""); return; } } catch (Exception e) { logger.error(e.getMessage()); } } // 返回信息 private void backInfo(HttpServletResponse response, boolean flag, int message, String fileName) { fileName=fileName.replace("\\","https://www.jb51.net/"); String json = ""; if (flag) { json = "{ \"status\": \"success"; } else { json = "{ \"status\": \"error"; } json += "\",\"fileName\": \"http://127.0.0.1:8090/file/show?fileName=" + fileName + "\",\"message\": \"" + message + "\"}"; try { response.setContentType("text/html;charset=utf-8"); response.getWriter().write(json); } catch (IOException e) { logger.error(e.getMessage(), e); } }

第三步:后台处理显示图片

/** * 显示单张图片 * @return */ @RequestMapping("/show") public ResponseEntity showPhotos(String fileName){ try { String path = "/Users/qinshengfei/fsdownload"; // 由于是读取本机的文件,file是一定要加上的, path是在application配置文件中的路径 logger.error("showPhotos:"+path+fileName); return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName)); } catch (Exception e) { return ResponseEntity.notFound().build(); } }

第四步:显示效果

vue+springboot图片上传和显示的示例代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/51e148c2d85b615fe0c58a3bf26badc4.html