添加删除图片接口

This commit is contained in:
liuchengqian 2022-03-09 10:40:08 +08:00
parent 49130281bb
commit b74dd02f87
6 changed files with 72 additions and 32 deletions

View File

@ -2,14 +2,12 @@ package com.xkrs.controller;
import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.PictureEntity;
import com.xkrs.model.qo.DeletePictureQo;
import com.xkrs.model.qo.PictureQo;
import com.xkrs.service.PictureService;
import com.xkrs.util.PictureQuery;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@ -40,16 +38,19 @@ public class PictureController {
return pictureService.insertPicture(pictureQo, filePicture);
}
/**
* 删除图片
*/
@PostMapping("/deletePicture")
public String deletePicture(@RequestBody DeletePictureQo deletePictureQo) {
return pictureService.deletePicture(deletePictureQo.getPicturePath());
}
/**
* 查询图片
*/
@GetMapping("/selectPicture")
public String selectPicture(@RequestParam(required = false, value = "lotNo") String lotNo,
@RequestParam(required = false, value = "machineNo") String machineNo,
@RequestParam(required = false, value = "materialNo") String materialNo,
@RequestParam(required = false, value = "modelNo") String modelNo,
@RequestParam(required = false, value = "machineTypeNo") String machineTypeNo,
@RequestParam(required = false, value = "craftItemName") String craftItemName) {
public String selectPicture(@RequestParam(required = false, value = "lotNo") String lotNo, @RequestParam(required = false, value = "machineNo") String machineNo, @RequestParam(required = false, value = "materialNo") String materialNo, @RequestParam(required = false, value = "modelNo") String modelNo, @RequestParam(required = false, value = "machineTypeNo") String machineTypeNo, @RequestParam(required = false, value = "craftItemName") String craftItemName) {
Locale locale = LocaleContextHolder.getLocale();
List<PictureEntity> pictureEntities = pictureQuery.selectPicture(lotNo, machineNo, materialNo, modelNo, machineTypeNo, craftItemName);
if (pictureEntities.isEmpty()) {

View File

@ -0,0 +1,30 @@
package com.xkrs.model.qo;
import com.xkrs.model.validation.DeletePictureQoDelete;
import javax.validation.constraints.NotBlank;
public class DeletePictureQo {
/**
* 图片路径
*/
@NotBlank(message = "{PictureEntity.picturePath.blank}", groups = {DeletePictureQoDelete.class})
private String picturePath;
public DeletePictureQo() {
}
public String getPicturePath() {
return picturePath;
}
public void setPicturePath(String picturePath) {
this.picturePath = picturePath;
}
@Override
public String toString() {
return "DeletePictureQo{" + "picturePath='" + picturePath + '\'' + '}';
}
}

View File

@ -0,0 +1,4 @@
package com.xkrs.model.validation;
public interface DeletePictureQoDelete {
}

View File

@ -16,4 +16,9 @@ public interface PictureService {
*/
String insertPicture(PictureQo pictureQo, MultipartFile pictureFile) throws IOException;
/**
* 删除图片
*/
String deletePicture(String picturePath);
}

View File

@ -37,4 +37,11 @@ public class PictureServiceImpl implements PictureService {
pictureDao.save(pictureEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
}
@Override
public String deletePicture(String picturePath) {
Locale locale = LocaleContextHolder.getLocale();
String deleteResult = ExcelUploadUtil.delFile(picturePath, 2);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, deleteResult, locale);
}
}

View File

@ -39,13 +39,13 @@ public class ExcelUploadUtil {
String suffix = originalFilename.substring(index);
//uuid生成新的文件名
String newName = UUID.randomUUID().toString() + suffix;
// File file = new File("/Users/liuchengqian/Desktop/DaJiang/" + subDir);
File file = new File("/home/sxy/server/industrial_measurement/" + subDir);
File file = new File("/Users/liuchengqian/Desktop/DaJiang/" + subDir);
// File file = new File("/home/sxy/server/industrial_measurement/" + subDir);
if (!file.exists()) {
file.mkdirs();
}
// String path = "/Users/liuchengqian/Desktop/DaJiang/" + subDir + newName;
String path = "/home/sxy/server/industrial_measurement/" + subDir + newName;
String path = "/Users/liuchengqian/Desktop/DaJiang/" + subDir + newName;
// String path = "/home/sxy/server/industrial_measurement/" + subDir + newName;
//实现上传
fileInput.transferTo(new File(path));
return "http://118.24.27.47:2088/" + subDir + newName;
@ -97,28 +97,21 @@ public class ExcelUploadUtil {
/**
* 删除本地或服务器储存的图片
*
* @param path
* @return
*/
public static String delFile(String path) {
String resultInfo = null;
public static String delFile(String path, int subDirType) {
String subDir = subDirType == 1 ? "excel/" : "picture/";
int lastIndexOf = path.lastIndexOf("/");
String imgPath = path.substring(lastIndexOf + 1, path.length());
System.out.println(imgPath);
imgPath = "/usr/local/etc/images/" + imgPath;
// img_path = "/usr/etc/images/Folder/" + img_path;
File file = new File(imgPath);
String fileName = path.substring(lastIndexOf + 1);
String filePath = "/Users/liuchengqian/Desktop/DaJiang/" + subDir + fileName;
// String filePath = "/home/sxy/server/industrial_measurement/" + subDir + fileName;
File file = new File(filePath);
if (file.exists()) {
if (file.delete()) {
resultInfo = "删除成功!";
} else {
resultInfo = "删除失败!";
return "删除成功!";
}
} else {
resultInfo = "文件不存在";
return "删除失败!";
}
return resultInfo;
return "文件不存在";
}
/**