|
|
@@ -119,51 +119,102 @@ public class CoinController {
|
|
|
* @author lvzhiqiang
|
|
|
* 2023/12/29 10:26
|
|
|
*/
|
|
|
- @RequestMapping("/uploadImgs")
|
|
|
+ @RequestMapping("/InsertOrUpdateImg")
|
|
|
@ResponseBody
|
|
|
- public R uploadImgs(@RequestParam("file") MultipartFile file, String remark, Long categoryId) {
|
|
|
- if (file == null || file.getSize() == 0) {
|
|
|
+ public R InsertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, Long categoryId, String id) {
|
|
|
+ if (StringUtils.isEmpty(id) && (file == null || file.getSize() == 0)) {
|
|
|
throw new ParameterException("文件为空!");
|
|
|
}
|
|
|
|
|
|
+ if (categoryId == null) {
|
|
|
+ throw new ParameterException("categoryId为空!");
|
|
|
+ }
|
|
|
+
|
|
|
String imageUrl = "";
|
|
|
String imageSize = "";
|
|
|
- try {
|
|
|
- // 1、给上传的图片生成新的文件名
|
|
|
- // 1.1获取原始文件名
|
|
|
- String oldName = file.getOriginalFilename();
|
|
|
- // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
- String newName = FtpUtil.genImageName();
|
|
|
- newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
-
|
|
|
- // 2、把图片上传到图片服务器
|
|
|
- // 2.1获取上传的io流
|
|
|
- InputStream input = file.getInputStream();
|
|
|
- // 2.2调用FtpUtil工具类进行上传
|
|
|
- boolean result = FtpUtil.uploadFile(newName, input);
|
|
|
- if (result) {
|
|
|
- //返回给前端图片访问路径
|
|
|
- imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
|
|
|
- imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
-
|
|
|
- FileImage fileImage = new FileImage();
|
|
|
- fileImage.setOldName(oldName);
|
|
|
- fileImage.setNewName(newName);
|
|
|
- fileImage.setSize(imageSize);
|
|
|
- fileImage.setPath(imageUrl);
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ try {
|
|
|
+ // 1、给上传的图片生成新的文件名
|
|
|
+ // 1.1获取原始文件名
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+
|
|
|
+ // 2、把图片上传到图片服务器
|
|
|
+ // 2.1获取上传的io流
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(newName, input);
|
|
|
+ if (result) {
|
|
|
+ //返回给前端图片访问路径
|
|
|
+ imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
+
|
|
|
+ FileImage fileImage = new FileImage();
|
|
|
+ fileImage.setOldName(oldName);
|
|
|
+ fileImage.setNewName(newName);
|
|
|
+ fileImage.setSize(imageSize);
|
|
|
+ fileImage.setPath(imageUrl);
|
|
|
+ fileImage.setRemark(remark);
|
|
|
+ fileImage.setCategoryId(categoryId);
|
|
|
+ coinMapper.insertFileImage(fileImage);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl);
|
|
|
+ result.put("imageSize", imageSize);
|
|
|
+
|
|
|
+ return R.ok().data(result);
|
|
|
+ } else {
|
|
|
+ FileImage fileImage = coinMapper.findFileImageById(Long.valueOf(id));
|
|
|
+ if (fileImage == null) {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ fileImage.setCategoryId(categoryId);
|
|
|
+ if (StringUtils.isNotEmpty(remark)) {
|
|
|
fileImage.setRemark(remark);
|
|
|
- fileImage.setCategoryId(categoryId);
|
|
|
- coinMapper.insertFileImage(fileImage);
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
|
|
|
- JSONObject result = new JSONObject();
|
|
|
- result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl);
|
|
|
- result.put("imageSize", imageSize);
|
|
|
+ if (file != null && file.getSize() > 0) {
|
|
|
+ try {
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
|
|
|
+ boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
|
|
|
+ if (flag) {
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(newName, input);
|
|
|
+ if (result) {
|
|
|
+ imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
+
|
|
|
+ fileImage.setOldName(oldName);
|
|
|
+ fileImage.setNewName(newName);
|
|
|
+ fileImage.setSize(imageSize);
|
|
|
+ fileImage.setPath(imageUrl);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return R.error().message(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return R.ok().data(result);
|
|
|
+ coinMapper.updateFileImage(fileImage);
|
|
|
+ return R.ok().data("success");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@RequestMapping("/deleteImgs/{imageId}")
|