//我的会员中心  头像上传接口
/*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

05-11 20:26