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