linux文件流上传,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();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值