@RequestMapping(value = "/File/save", method = RequestMethod.POST)@ResponseBodypublic ResponseResult<Boolean> insertTbInspectFile(MultipartFile file, String uploadUser) {log.info("执行添加巡查图片视频。传入的参数信息为: filePath = " + file.getOriginalFilename()+ " uploadUser= " + uploadUser);if (file.isEmpty()) {return ResultUtils.error("图片不能为空");}if (uploadUser == null || uploadUser.equals("")) {return ResultUtils.error("上传人不能为空");}String path = "";long startTime = System.currentTimeMillis();//获取图片名称String filePath = file.getOriginalFilename();log.info("文件名称:" + filePath);//截取文件后缀int lastIndexOf = filePath.lastIndexOf(".");String fileType = filePath.substring(lastIndexOf + 1);log.info("文件名类型:{}", fileType);//文件类型判断 jpg,pngif (fileType.equals("jpg") || fileType.equals("png")) {//fileimgPath从配置文件中读取的路径信息,如'inspect.file.path=E:\\img',获取路径@Value("${inspect.file.path}")log.info("文件保存位置:" + fileimgPath);File imagePath = new File(fileimgPath);if (!imagePath.exists()) {//不存在,创建imagePath.mkdir();}//时间戳+图片名称path = imagePath.getPath() + "\\" + new Date().getTime() + filePath;File newFile = new File(path);//通过CommonsMultipartFile的方法直接写文件(注意这个时候)try {//文件上传file.transferTo(newFile);long endTime = System.currentTimeMillis();log.info("采用file.Transto的运行时间:" + String.valueOf(endTime - startTime) + "ms");} catch (IOException e) {e.printStackTrace();}} else {return ResultUtils.error("不允许该" + fileType + "文件类型上传");}//保存数据库,insertTbInspectFile保存方法,正常数据库写法就可以if (tbInspectFileService.insertTbInspectFile(path,uploadUser) > 0) {log.info("图片上传成功。");return ResultUtils.ok(true);}return ResultUtils.error("服务器内部发生错误,用户添加失败,请稍后重试。");}@RequestMapping(value = "/File/deleteId", method = RequestMethod.POST)@ResponseBodypublic ResponseResult<Boolean> deleteTbInspectFile(Integer id) {log.info("删除图片,相关参数为id= " + id);if (id == null || id.equals("")) {return ResultUtils.error("编号不能为空");}//根据ID查询图片信息List<TbInspectFile> list = tbInspectFileService.selectTbInspectFileId(id);if (list != null && list.size() > 0) {//根据查出的图片路径删除本地文件new File(list.get(0).getFilePath()).delete();//删除数据库中图片数据tbInspectFileService.deleteTbInspectFile(id);}return ResultUtils.ok(true);}
900字范文,内容丰富有趣,生活中的好帮手!