1 //我的會員中心 頭像上傳接口 2 /*windows 調試*/ 3 @Value("${appImg.location}") 4 private String winPathPic; 5 /*linux 使用*/ 6 @Value("${img.location}") 7 private String linuxPathPic; 8 9 @PostMapping(value = "/file") 10 public String file() { 11 return "file"; 12 } 13 14 @ApiOperation(value = "會員頭像文件上傳") 15 @ApiImplicitParams({ 16 }) 17 @RequestMapping(value = "/appMbrImgUploadFile", method = RequestMethod.POST) 18 @ResponseBody 19 public Object appMbrImgUploadFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) { 20 Subject currentUser = SecurityUtils.getSubject(); 21 ShiroUser shiroUser = null; 22 Session session = currentUser.getSession(); 23 String mobile = session.getAttribute("username") == null ? "" : String.valueOf(session.getAttribute("username")); 24 if (mobile.equals("")) { 25 return setModelMap(new ModelMap(), HttpCode.EFFECT_OF_TOKEN.value(), "token已經失效,請登錄", null); 26 } 27 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 28 //時間路徑 29 String fileTime = sdf.format(new Date()); 30 // 文件名 31 String fileName = file.getOriginalFilename(); 32 // 后綴名 33 String suffixName = fileName.substring(fileName.lastIndexOf(".")); 34 //圖片格式校驗 35 String reg = "(.png)$"; 36 Pattern pattern = Pattern.compile(reg); 37 Matcher matcher = pattern.matcher(suffixName); 38 boolean valiRst = matcher.find(); 39 if (!valiRst) { 40 return setModelMap(new ModelMap(), HttpCode.PICTURE_FORMAT_CHECK_EXCEPTION.value(), "圖片格式異常", null); 41 } 42 if (file.isEmpty()) { 43 System.out.println("文件為空"); 44 logger.error("文件流為空異常"); 45 return setModelMap(new ModelMap(), HttpCode.FILE_STREAM_EMPTY.value(), "文件流為空異常", null); 46 } 47 //判斷操作系統 48 String os = System.getProperty("os.name"); 49 System.out.println(os); 50 //定義路徑 51 String picPathReal=""; 52 if(os!=null&&os.contains("Windows 7")){ 53 System.out.println("Windows 7"); 54 picPathReal=winPathPic; 55 }else{ 56 //linux 57 picPathReal=linuxPathPic; 58 } 59 // 新文件名 60 fileName = UUID.randomUUID() + suffixName; 61 // 文件目錄 62 String diectFile=picPathReal+"/"+fileTime; 63 File dest = new File(diectFile); 64 dest.setWritable( true, false); 65 if (!dest.exists()) { 66 dest.mkdirs(); 67 } 68 //創建文件 圖片 69 File filePic = new File(diectFile+File.separator, fileName); 70 try { 71 file.transferTo(filePic); 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } 75 LSysMember sysMember = new LSysMember(); 76 LSysMemberVo userInfo = new LSysMemberVo(); 77 //返回指定圖片文件路徑 完整接口地址 78 //保存地址處理 79 String savePath="/lSysMember/noLogin/readImageFile?url="+picPathReal+File.separator+fileTime+File.separator+fileName; 80 //String filename = "/lSysMember/noLogin/readImageFile?url="+savePath+File.separator + fileTime + File.separator + fileName; 81 //查詢會員基本信息 82 sysMember = memberService.findLoginMemberInfo(mobile); 83 //執行更新頭像更新操作 84 sysMember.setAvatar(savePath); 85 try { 86 sysMember = memberService.appMbrImgUploadFile(sysMember); 87 userInfo = memberService.findLoginMemberInfoByMobile(mobile); 88 } catch (Exception e) { 89 logger.error("請求異常----", e); 90 throw new CallChainException(); 91 } 92 //更新session 93 session.setAttribute("shiroUser", userInfo); 94 return setSuccessModelMap(savePath); 95 } 96 97 98 @ApiOperation(value = "返回指定地址的文件流") 99 @ApiImplicitParams({ 100 @ApiImplicitParam(name = "url", value = "圖片地址", required = true, 101 paramType = "query", defaultValue = "\\20180912\\7cd2e1a3-a087-4e25-aac8-2bdf8e274c6f.png"), 102 }) 103 @RequestMapping(value = "/noLogin/readImageFile",method =RequestMethod.GET) 104 @ResponseBody 105 public void getUrlFile(String url, HttpServletRequest request, HttpServletResponse response) { 106 File file = new File(url); 107 // 后綴名 108 String suffixName = url.substring(url.lastIndexOf(".")); 109 //判斷文件是否存在如果不存在就返回默認圖標 110 if (!(file.exists() && file.canRead())) { 111 file = new File(request.getSession().getServletContext().getRealPath("/") 112 + "resource/icons/auth/root.png"); 113 } 114 FileInputStream inputStream = null; 115 try { 116 inputStream = new FileInputStream(file); 117 byte[] data = new byte[(int) file.length()]; 118 int length = inputStream.read(data); 119 inputStream.close(); 120 //setContentType("text/plain; charset=utf-8"); 文本 121 response.setContentType("image/png;charset=utf-8"); 122 OutputStream stream = response.getOutputStream(); 123 stream.write(data); 124 stream.flush(); 125 stream.close(); 126 } catch (FileNotFoundException e) { 127 e.printStackTrace(); 128 } catch (IOException e) { 129 e.printStackTrace(); 130 } 131 }
來源: https://www.cnblogs.com/javajetty/p/9655612.html
http://www.bubuko.com/infodetail-2366518.html