标签:ice amp root current etc bubuko length reg object
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
springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
标签:ice amp root current etc bubuko length reg object
原文地址:https://www.cnblogs.com/But-you/p/10781647.html