springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
//我的会员中心 头像上传接口
/*windows 调试*/
@Value("${appImg.location}")
private String winPathPic;
/*linux 使用*/
@Value("${img.location}")
private String linuxPathPic; @PostMapping(value = "/file")
public String file() {
return "file";
} @ApiOperation(value = "会员头像文件上传")
@ApiImplicitParams({
})
@RequestMapping(value = "/appMbrImgUploadFile", method = RequestMethod.POST)
@ResponseBody
public Object appMbrImgUploadFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
Subject currentUser = SecurityUtils.getSubject();
ShiroUser shiroUser = null;
Session session = currentUser.getSession();
String mobile = session.getAttribute("username") == null ? "" : String.valueOf(session.getAttribute("username"));
if (mobile.equals("")) {
return setModelMap(new ModelMap(), HttpCode.EFFECT_OF_TOKEN.value(), "token已经失效,请登录", null);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//时间路径
String fileTime = sdf.format(new Date());
// 文件名
String fileName = file.getOriginalFilename();
// 后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//图片格式校验
String reg = "(.png)$";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(suffixName);
boolean valiRst = matcher.find();
if (!valiRst) {
return setModelMap(new ModelMap(), HttpCode.PICTURE_FORMAT_CHECK_EXCEPTION.value(), "图片格式异常", null);
}
if (file.isEmpty()) {
System.out.println("文件为空");
logger.error("文件流为空异常");
return setModelMap(new ModelMap(), HttpCode.FILE_STREAM_EMPTY.value(), "文件流为空异常", null);
}
//判断操作系统
String os = System.getProperty("os.name");
System.out.println(os);
//定义路径
String picPathReal="";
if(os!=null&&os.contains("Windows 7")){
System.out.println("Windows 7");
picPathReal=winPathPic;
}else{
//linux
picPathReal=linuxPathPic;
}
// 新文件名
fileName = UUID.randomUUID() + suffixName;
// 文件目录
String diectFile=picPathReal+"/"+fileTime;
File dest = new File(diectFile);
dest.setWritable( true, false);
if (!dest.exists()) {
dest.mkdirs();
}
//创建文件 图片
File filePic = new File(diectFile+File.separator, fileName);
try {
file.transferTo(filePic);
} catch (IOException e) {
e.printStackTrace();
}
LSysMember sysMember = new LSysMember();
LSysMemberVo userInfo = new LSysMemberVo();
//返回指定图片文件路径 完整接口地址
//保存地址处理
String savePath="/lSysMember/noLogin/readImageFile?url="+picPathReal+File.separator+fileTime+File.separator+fileName;
//String filename = "/lSysMember/noLogin/readImageFile?url="+savePath+File.separator + fileTime + File.separator + fileName;
//查询会员基本信息
sysMember = memberService.findLoginMemberInfo(mobile);
//执行更新头像更新操作
sysMember.setAvatar(savePath);
try {
sysMember = memberService.appMbrImgUploadFile(sysMember);
userInfo = memberService.findLoginMemberInfoByMobile(mobile);
} catch (Exception e) {
logger.error("请求异常----", e);
throw new CallChainException();
}
//更新session
session.setAttribute("shiroUser", userInfo);
return setSuccessModelMap(savePath);
} @ApiOperation(value = "返回指定地址的文件流")
@ApiImplicitParams({
@ApiImplicitParam(name = "url", value = "图片地址", required = true,
paramType = "query", defaultValue = "\\20180912\\7cd2e1a3-a087-4e25-aac8-2bdf8e274c6f.png"),
})
@RequestMapping(value = "/noLogin/readImageFile",method =RequestMethod.GET)
@ResponseBody
public void getUrlFile(String url, HttpServletRequest request, HttpServletResponse response) {
File file = new File(url);
// 后缀名
String suffixName = url.substring(url.lastIndexOf("."));
//判断文件是否存在如果不存在就返回默认图标
if (!(file.exists() && file.canRead())) {
file = new File(request.getSession().getServletContext().getRealPath("/")
+ "resource/icons/auth/root.png");
}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
int length = inputStream.read(data);
inputStream.close();
//setContentType("text/plain; charset=utf-8"); 文本
response.setContentType("image/png;charset=utf-8");
OutputStream stream = response.getOutputStream();
stream.write(data);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
来源: https://www.cnblogs.com/javajetty/p/9655612.html
http://www.bubuko.com/infodetail-2366518.html
springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux的更多相关文章
- java导出excel并且压缩成zip上传到oss,并下载,使用字节流去存储,不用文件流保存文件到本地
最近项目上要求实现导出excel并根据条数做分割,然后将分割后的多个excel打包成压缩包上传到oss服务器上,然后提供下载方法,具体代码如下:这里只展示部分代码,获取数据的代码就不展示了 ByteA ...
- SpringBoot - 实现文件上传2(多文件上传、常用上传参数配置)
在前文中我介绍了 Spring Boot 项目如何实现单文件上传,而多文件上传逻辑和单文件上传基本一致,下面通过样例进行演示. 多文件上传 1,代码编写 1)首先在 static 目录中创建一个 up ...
- vue头像上传与文件压缩
工作中遇到的问题记录:vue开发头像上传组件,后端提供接口,需求为可相册上传,可相机拍摄上传,文件大小限制为2M 需求点分析 移动端调用相册/摄像头实现拍照 图片压缩,当前高像素的相机拍出来的图片都有 ...
- Html5实现头像上传和编辑,保存为Base64的图片过程
一.Html5实现头像上传和编辑 插件地址: html5手机端裁剪图片上传头像代码 本地项目引入注意事项: 1.将html的js搬到外面的js文件中,便于管理 2.图片样式在html都是在页面写死,需 ...
- Android连接socket服务器上传下载多个文件
android连接socket服务器上传下载多个文件1.socket服务端SocketServer.java public class SocketServer { ;// 端口号,必须与客户端一致 ...
- PHP上传(单个)文件示例
通过 PHP,可以把文件上传到服务器. 创建一个文件上传表单 允许用户从表单上传文件是非常有用的. 请看下面这个供上传文件的 HTML 表单: <html> <body> &l ...
- 聊一聊jquery文件上传(支持多文件上传)
谈到文件上传,现在一般都用现成的组件可以使用.PC端的可以使用uploadify.针对微网站H5也有uploadifive.但是这组件并不能满足各种场景的需求,例如:预览 切图 放大缩小,取消之类的. ...
- SharePoint 2010 ——自定义上传页面与多文件上传解决方案
最近项目遇到一个很麻烦的问题,原以为很容易解决,结果搞了那么久,先开个头,再慢慢写 SharePoint 2010 ——自定义上传页面与多文件上传解决方案 1.创建Sharepoint空白项目,创建应 ...
- .Net neatupload上传控件实现文件上传的进度条
1. 引入bin文件 (可以到neatupload官网下载,也可以到教育厅申报系统中找) 2. 将控件加入到工具栏,在工具栏中点鼠标右键,如图: 3. 加入neatuplaod这个文件夹(可以到nea ...
随机推荐
- SQL虚拟数字辅助表
虚拟数字辅助表是一个整数序列,可以用来完成多种不同的任务,如生成日期跟时间值序列,及分裂值列表.要用查询逻辑产生一个大的整数序列,可以使用交叉连接(cross join). 交叉联接(cross jo ...
- javascript总结34 :DOM之节点元素获取
常用节点元素获取: 1. 获取 html -- > document.documentElement 2. 获取 body -- > document.body 3. 获取指定的元素 -- ...
- Exception (3) Java exception handling best practices
List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 深入理解最强桌面地图控件GMAP.NET ---[更新]百度地图
之前写了篇博文,深入理解最强桌面地图控件GMAP.NET --- 百度地图 但是很多回复说百度地图更新了,不能显示百度的离线地图.之前承诺说是国庆节更新,最近才更新.代码已经提交到: https:// ...
- Verilog MIPS32 CPU(二)-- Regfiles
Verilog MIPS32 CPU(一)-- PC寄存器 Verilog MIPS32 CPU(二)-- Regfiles Verilog MIPS32 CPU(三)-- ALU Verilog M ...
- 使用纯真IP库获取用户端地理位置信息
引言 在一些电商类或者引流类的网站中经常会有获取用户地理位置信息的需求,下面我分享一个用纯真IP库获取用户地理位置信息的方案. 正文 第一步:本文的方案是基于纯真IP库的,所以首先要去下载最新的纯真I ...
- docker安装与基本配置
Docker有很多种安装的选择,我们推荐您在Ubuntu下面安装,因为docker是在Ubuntu下面开发的,安装包测试比较充分,可以保证软件包的可用性.Mac, windows和其他的一些linux ...
- 初学python - 脚本文件
解析: 第一行 #!/usr/bin/env python - py脚本运行环境[用python解释器解释脚本文件-对应python安装路径] 第二行 #-*-coding:utf-8-*- - ...
- How to Mount a Remote Folder using SSH on Ubuntu
Connecting to a server across the internet is much more secure using SSH. There is a way that you ca ...