@RequestMapping(value = "/File/save", method = RequestMethod.POST)
@ResponseBody
public 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);
if (fileType.equals("jpg") || fileType.equals("png")) {
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);
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 + "文件类型上传");
}
if (tbInspectFileService.insertTbInspectFile(path,uploadUser) > 0) {
log.info("图片上传成功。");
return ResultUtils.ok(true);
}
return ResultUtils.error("服务器内部发生错误,用户添加失败,请稍后重试。");
}
@RequestMapping(value = "/File/deleteId", method = RequestMethod.POST)
@ResponseBody
public ResponseResult<Boolean> deleteTbInspectFile(Integer id) {
log.info("删除图片,相关参数为id= " + id);
if (id == null || id.equals("")) {
return ResultUtils.error("编号不能为空");
}
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);
}