From 266c46fe16e481df797d39dc33c0cbc10d6111a6 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 1 Mar 2022 14:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8F=82=E6=95=B0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/controller/FileController.java | 2 +- src/main/java/com/xkrs/dao/PictureDao.java | 3 +- .../xkrs/service/impl/FileServiceImpl.java | 2 +- .../xkrs/service/impl/PictureServiceImpl.java | 2 +- .../java/com/xkrs/util/ExcelUploadUtil.java | 46 ++++++++++--------- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/xkrs/controller/FileController.java b/src/main/java/com/xkrs/controller/FileController.java index d47dfec..2e1ca5e 100644 --- a/src/main/java/com/xkrs/controller/FileController.java +++ b/src/main/java/com/xkrs/controller/FileController.java @@ -59,7 +59,7 @@ public class FileController { @Transactional(rollbackOn = Exception.class) public String updateFileUploadPath(@RequestBody FileUpdateQo fileUpdateQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { Locale locale = LocaleContextHolder.getLocale(); - String file = ExcelUploadUtil.memoryFile(fileExcel); + String file = ExcelUploadUtil.memoryFile(fileExcel,1); fileDao.updateFileUploadPath(fileUpdateQo.getLotNo(), fileUpdateQo.getMachineNo(), fileUpdateQo.getMaterialNo(), fileUpdateQo.getModelNo(), fileUpdateQo.getPartNo(), file); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功!", locale); } diff --git a/src/main/java/com/xkrs/dao/PictureDao.java b/src/main/java/com/xkrs/dao/PictureDao.java index 5118456..56f8661 100644 --- a/src/main/java/com/xkrs/dao/PictureDao.java +++ b/src/main/java/com/xkrs/dao/PictureDao.java @@ -7,7 +7,6 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Component; -import java.util.List; import java.util.Optional; /** @@ -41,6 +40,6 @@ public interface PictureDao extends JpaRepository, JpaSpeci * @param picturePath 图片路径 */ @Modifying(clearAutomatically = true) - @Query(value = "UPDATE qc_inspection_picture SET picture_path = ?6 where lot_no = ?1 and machine_no = ?2 and material_no = ?3 and model_no = ?4 and part_no = ?5",nativeQuery = true) + @Query(value = "UPDATE qc_inspection_picture SET picture_path = ?6 where lot_no = ?1 and machine_no = ?2 and material_no = ?3 and model_no = ?4 and part_no = ?5", nativeQuery = true) void updatePicturePath(String lotNo, String machineNo, String materialNo, String modelNo, String partNo, String picturePath); } diff --git a/src/main/java/com/xkrs/service/impl/FileServiceImpl.java b/src/main/java/com/xkrs/service/impl/FileServiceImpl.java index fd1a470..3a6319e 100644 --- a/src/main/java/com/xkrs/service/impl/FileServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FileServiceImpl.java @@ -54,7 +54,7 @@ public class FileServiceImpl implements FileService { if (fileExcel == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "模板不能为空!", locale); } - String file = ExcelUploadUtil.memoryFile(fileExcel); + String file = ExcelUploadUtil.memoryFile(fileExcel,1); FileEntity fileEntity1 = new FileEntity(); fileEntity1.setLotNo(fileQo.getLotNo()); fileEntity1.setMachineNo(fileQo.getMachineNo()); diff --git a/src/main/java/com/xkrs/service/impl/PictureServiceImpl.java b/src/main/java/com/xkrs/service/impl/PictureServiceImpl.java index 7e5ca48..c992243 100644 --- a/src/main/java/com/xkrs/service/impl/PictureServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/PictureServiceImpl.java @@ -35,7 +35,7 @@ public class PictureServiceImpl implements PictureService { if (pictureEntity2.isPresent()) { return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "请勿重复添加!", locale); } - String file = ExcelUploadUtil.memoryFile(pictureFile); + String file = ExcelUploadUtil.memoryFile(pictureFile, 2); PictureEntity pictureEntity1 = new PictureEntity(); pictureEntity1.setLotNo(lotNo); pictureEntity1.setMachineNo(machineNo); diff --git a/src/main/java/com/xkrs/util/ExcelUploadUtil.java b/src/main/java/com/xkrs/util/ExcelUploadUtil.java index 4e863f6..d98ca96 100644 --- a/src/main/java/com/xkrs/util/ExcelUploadUtil.java +++ b/src/main/java/com/xkrs/util/ExcelUploadUtil.java @@ -23,16 +23,18 @@ public class ExcelUploadUtil { /** * 上传单张图片 - * @param fileExcel + * + * @param fileInput * @return * @throws IOException */ - public static String memoryFile(MultipartFile fileExcel) throws IOException { + public static String memoryFile(MultipartFile fileInput, int subDirType) throws IOException { + String subDir = subDirType == 1 ? "excel/" : "picture/"; //String uploadPath = "http://139.199.98.175:2088/wfTaskImage/"; // String uploadPath = "http://192.168.2.9:2088/"; String uploadPath = "http://118.24.27.47:4096/"; //获取原始文件名 - String originalFilename = fileExcel.getOriginalFilename(); + String originalFilename = fileInput.getOriginalFilename(); if (originalFilename != null && !"".equals(originalFilename)) { //找到 . 的位置 int index = originalFilename.lastIndexOf("."); @@ -42,16 +44,16 @@ public class ExcelUploadUtil { String newName = UUID.randomUUID().toString() + suffix; //将图片保存到本地/usr/etc/images/Folder - //File file = new File("E:/shoptest/"); - File file = new File("/home/sxy/server/industrial_measurement/excel/"); +// File file = new File("/Users/liuchengqian/Desktop/DaJiang/"); + File file = new File("/home/sxy/server/industrial_measurement/" + subDir); if (!file.exists()) { file.mkdirs(); } - //String path = "E:/shoptest/" + newName; - String path = "/home/sxy/server/industrial_measurement/excel/" + newName; +// String path = "/Users/liuchengqian/Desktop/DaJiang/"+newName; + String path = "/home/sxy/server/industrial_measurement/" + subDir + newName; String uploadsImage = uploadPath + newName; //实现上传 - fileExcel.transferTo(new File(path)); + fileInput.transferTo(new File(path)); return path; } return null; @@ -99,27 +101,27 @@ public class ExcelUploadUtil { }*/ - /** * 删除本地或服务器储存的图片 + * * @param path * @return */ - public static String delFile(String path){ + public static String delFile(String path) { String resultInfo = null; int lastIndexOf = path.lastIndexOf("/"); - String imgPath = path.substring(lastIndexOf + 1,path.length()); + 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); - if(file.exists()){ - if(file.delete()){ + if (file.exists()) { + if (file.delete()) { resultInfo = "删除成功!"; - }else { + } else { resultInfo = "删除失败!"; } - }else { + } else { resultInfo = "文件不存在"; } return resultInfo; @@ -127,6 +129,7 @@ public class ExcelUploadUtil { /** * 通过图片路径解析 ,上传保存 + * * @param listImgSrc * @return */ @@ -171,17 +174,18 @@ public class ExcelUploadUtil { /** * 删除本地文件夹图片 + * * @param url */ - public static void deleteImage(String url){ - File file=new File(url); + public static void deleteImage(String url) { + File file = new File(url); //判断file是否是文件目录 若是返回TRUE - if (file.isDirectory()){ + if (file.isDirectory()) { //name存储file文件夹中的文件名 - String[] name =file.list(); - for (int i=0; i