springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux...
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
转载于:https://www.cnblogs.com/But-you/p/10781647.html
springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux...相关推荐
- SpringBoot分片上传、断点续传、大文件极速秒传功能(典藏版)
SpringBoot分片上传.断点续传.大文件极速秒传功能,这篇都帮你搞定!(典藏版) Java研发军团 2023-02-03 21:00 文件上传是一个老生常谈的话题了,在文件相对比较小的情况下,可 ...
- nodejs实现文件/头像上传
文件上传 文件上传就是把图片.音视频等文件上传到服务端,文件上传必须使用post请求,数据格式为multipart/form-data的数据. 服务端 在nodejs中处理文件,需要使用multer模 ...
- php文件 用户头像上传代码,网页web上传用户头像代码实现(美图秀秀开放)
网页web上传用户头像代码实现(美图秀秀开放) 在制作论坛或者一些门户社交网站的时候,经常要获取用户的头像.之前我们一般都是自己制作flash插件头像上传.或者用js来自己开发一个头像上传功能.比如有 ...
- jsp文件上传 头像上传
前言: 这篇文章简单介绍了文件上传的代码和我遇到的一些问题以及基于我实现的头像上传(不包含框架,仅仅达到目标即可) 一.整体效果: (1)头像显示 (2)文件上传 (3)完成头像修改 (4)查看文件目 ...
- springboot实现上传Excel文件与数据库中的数据进行比对
springboot实现上传Excel文件与数据库中的数据进行比对 首先先写好文件上传的接口,然后上传需要比对数据的文件,在点击数据比对 下面是一部分数据比对的代码: 后端controller部分: ...
- 解决文件上传_使用FastDfs上传头像上传不成功的问题---SpringCloud Alibaba_若依微服务框架改造---工作笔记002
在若依的微服务版框架中,使用了vue-cropper 截图组件,进行头像的上传 关于vue-cropper 截图组件的使用网上一大堆. 但是发现如果把文件上传途径切换为,采用FastDfs文件上传的会 ...
- SpringBoot如何上传大文件
最近遇见一个需要上传超大大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...
- layui文件上传(头像上传)
头像上传 头像上传大概流程就是选择电脑上的文件图片然后上传到服务器服务器存起来然后上传到浏览器中,服务器需要用到磁盘存储模块(multer),需要下载安装 multer是Nodejs中用于处理文件上传 ...
- SpringBoot 项目上传文件异常【java.io.IOException: Stream closed】
项目场景: 提示:这里简述项目相关背景: 项目场景:SpringBoot 项目上传文件接口异常 21 十二月 2022 13:30:53,132 36991 [http-nio-9220-exec-3 ...
最新文章
- 配置EXCHANGE服务器
- 多维数组和C#中的数组数组有什么区别?
- asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解
- 直接插入排序,折半插入排序,希尔排序,简单选择排序,冒泡排序,快速排序模板以及比较次数与移动次数的分析,折半搜索算法模板
- linux 读取内存颗粒,linux查看主板内存槽与内存信息的命令dmidecode怎么用
- 中国企业海外人才发展白皮书
- cacti mysql-bin_Cacti环境搭建(LNMP环境)
- anaconda pycharm_搭建 Python 高效开发环境: Pycharm + Anaconda
- linux mint 1.9 qq 安装
- java spring多数据源配置文件_基于注解实现SpringBoot多数据源配置
- CSS样式(一)- 基本语法
- vb调用存储过程的方法
- 【深入浅出imx8企业级开发实战 | 01】imx8qxp yocto工程构建指南
- 大伽「趣」说AI:在多个场景中的AI落地实践
- python获取当前计算机cpu数量
- java面试题干货51-95
- 阿朱访谈:程序员转型期职业选择,是继续做技术高手还...(转)
- 无人驾驶学习介绍和感悟
- 《每日论文》ImageNet Classification with Deep Convolutional Neural Networks
- 教大家如何清理C盘~
热门文章
- dhtmlxgrid表格笔记
- Babel 相关资料
- CSS里总算是有了一种简单的垂直居中布局的方法了
- Linux的rc.local自启动服务
- Mr. Process的一生-Linux内核的社会视角 (2)启动
- 切换apache的prefork和worker模式
- 好多Javascript日期选择器呀-5
- 如何在DataGrid里面产生滚动条而不滚动题头
- python脚本 数据库压力测试_python-网站压力测试脚本
- unity2018关联不到vs_律道|蓝月传奇VS烈焰武尊:角色扮演类游戏独创性如何认定?...