| | |
| | | redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS); |
| | | return AjaxResult.success(imgstr); |
| | | } |
| | | @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名") |
| | | @PostMapping(value = "uploadManage", headers = "content-type=multipart/form-data") |
| | | public AjaxResult uploadManage(@RequestParam(value = "file") MultipartFile mf) throws IOException { |
| | | Long userId = tokenService.getLoginUser().getUserid(); |
| | | if(null == userId){ |
| | | return AjaxResult.error("请先登录"); |
| | | } |
| | | if (mf.isEmpty()) { |
| | | return AjaxResult.error("请传入文件!"); |
| | | } |
| | | String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | String realPath = fileUploadConfig.getLocation() + TimeDir; |
| | | File file = new File(realPath); |
| | | // 没有目录就创建 |
| | | if (!file.exists()) { |
| | | file.mkdirs(); |
| | | } |
| | | // 获取文件名称 |
| | | String filename = mf.getOriginalFilename(); |
| | | if(filename.contains("../")){ |
| | | filename = filename.replaceAll("\\.\\./", ""); |
| | | } |
| | | // 获取文件后缀 |
| | | String ext = filename.substring(filename.lastIndexOf(".") + 1); |
| | | // 检查文件类型 |
| | | if (!fileUploadConfig.getAllowExt().contains(ext)) { |
| | | return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt()); |
| | | } |
| | | filename = UUID.randomUUID() + "." + ext; |
| | | File targetFile = new File(realPath, filename);//目标文件 |
| | | //开始从源文件拷贝到目标文件 |
| | | //传图片一步到位 |
| | | mf.transferTo(targetFile); |
| | | //拼接数据 |
| | | String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ filename; |
| | | redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS); |
| | | return AjaxResult.success(imgstr); |
| | | } |
| | | |
| | | |
| | | // @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名") |