上传图片到linux返回url,springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux...

1 //我的会员中心 头像上传接口

2 /*windows 调试*/

3 @Value("${appImg.location}")4 privateString winPathPic;5 /*linux 使用*/

6 @Value("${img.location}")7 privateString linuxPathPic;8

9 @PostMapping(value = "/file")10 publicString file() {11 return "file";12 }13

14 @ApiOperation(value = "会员头像文件上传")15 @ApiImplicitParams({16 })17 @RequestMapping(value = "/appMbrImgUploadFile", method =RequestMethod.POST)18 @ResponseBody19 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(newDate());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 = newFile(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 = newLSysMember();76 LSysMemberVo userInfo = newLSysMemberVo();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 newCallChainException();91 }92 //更新session

93 session.setAttribute("shiroUser", userInfo);94 returnsetSuccessModelMap(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 @ResponseBody105 public voidgetUrlFile(String url, HttpServletRequest request, HttpServletResponse response) {106 File file = newFile(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 = newFileInputStream(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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值