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 ...
随机推荐
- mysql 空间索引的使用
CREATE TABLE tb_geo(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(128) NOT NULL,pnt POINT NOT NULL, ...
- left join、right join、inner join、full join
转自:某一网友 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join ...
- 利用predis操作redis方法大全
predis是PHP连接Redis的操作库,由于它完全使用php编写,大量使用命名空间以及闭包等功能,只支持php5.3以上版本,故实测性能一般,每秒25000次读写. 将session数据存放到re ...
- java.awt.Graphics2D 图片缩放
关键字:java image thumbnail google 粗略demo: import java.awt.Graphics2D; import java.awt.GraphicsConfig ...
- mysql多字段唯一索引
项目中需要用到联合唯一索引: 例如:有以下需求:每个人每一天只有可能产生一条记录:处了程序约定之外,数据库本身也可以设定: 例如:user表中有userID,userName两个字段,如果不希望有2条 ...
- Android-Style样式
说到Style样式在,HTML+Javascript+CSS中,CSS就是样式,样式可以把很多通用到效果,统一为一个样式,达到通用的目的,也可以让代码更加简洁. 什么时候用Style样式 ? 例如:A ...
- [CentOS]使用yum命令报出Error: Cannot retrieve repository metadata (repomd.xml) for repository的解决方法
在一次错误的repo文件rpm -i 之后,执行yum就开始报出 Error: Cannot retrieve repository metadata (repomd.xml) for reposit ...
- jquery 实现抖动效果
jQuery.fn.shake = function (intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDurat ...
- Windowsform datagridview选中向父窗口传值
Datagridview mouseclick事件 private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { i ...
- consul ACL2
简介 Consul有多个组件,但是整体上,consul通常作为服务发现工具来使用. Consul主要由以下特点: 服务发现 健康检查 KV存储 多数据中心 Consul一般与zookeeper,ser ...