diff --git a/common/src/main/java/com/mh/common/utils/FileUtils.java b/common/src/main/java/com/mh/common/utils/FileUtils.java index 0bbb80b..9cb014d 100644 --- a/common/src/main/java/com/mh/common/utils/FileUtils.java +++ b/common/src/main/java/com/mh/common/utils/FileUtils.java @@ -6,6 +6,9 @@ import sun.misc.BASE64Encoder; import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; /** * 文件相关操作 @@ -104,4 +107,21 @@ public class FileUtils { throw new RuntimeException(e); } } + + public static void copyFile(MultipartFile file, String uploadDir) { + // 保存图片到本地 + try { + // 确保目标目录存在 + File dir = new File(uploadDir); + if (!dir.exists()) { + dir.mkdirs(); + } + // 文件保存路径 + Path path = Paths.get(uploadDir + file.getOriginalFilename()); + // 将上传文件保存到指定路径 + Files.copy(file.getInputStream(), path); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/user-service/src/main/java/com/mh/user/constants/Constant.java b/user-service/src/main/java/com/mh/user/constants/Constant.java index c9e903c..3ba79f5 100644 --- a/user-service/src/main/java/com/mh/user/constants/Constant.java +++ b/user-service/src/main/java/com/mh/user/constants/Constant.java @@ -47,4 +47,7 @@ public class Constant { // 压力值 public static String PRE = "0012"; + // 项目图片存储路径 + public static String PROJECT_IMG_PATH = "\\images\\"; + } diff --git a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java index 481cdb4..1c81d71 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java @@ -90,6 +90,8 @@ public class MeterManageServiceImpl implements MeterManageService { entity.setMtCode(ExchangeStringUtil.addZeroForNum(entity.getMtCode(), 12)); } meterManageMapper.updateById(entity); + // 更新对应的设备采集参数 + deviceCodeParamService.createCodeParams(entity); } @Override @@ -106,12 +108,15 @@ public class MeterManageServiceImpl implements MeterManageService { entity.setMtCode(ExchangeStringUtil.addZeroForNum(entity.getMtCode(), 12)); } meterManageMapper.insert(entity); + // 更新对应的设备采集参数 + deviceCodeParamService.createCodeParams(entity); } @Override public void delete(String id) { MeterManageEntity meterManageEntity = meterManageMapper.selectById(id); meterManageMapper.deleteById(id); + // 更新对应的设备采集参数 deviceCodeParamService.createCodeParams(meterManageEntity); } diff --git a/user-service/src/main/java/com/mh/user/service/impl/ProjectInfoServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/ProjectInfoServiceImpl.java index 0b8af95..e17fd4c 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/ProjectInfoServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/ProjectInfoServiceImpl.java @@ -7,6 +7,7 @@ import com.mh.common.page.PageRequest; import com.mh.common.page.PageResult; import com.mh.common.utils.FileUtils; import com.mh.common.utils.StringUtils; +import com.mh.user.constants.Constant; import com.mh.user.entity.ProjectInfoEntity; import com.mh.user.mapper.ProjectInfoMapper; import com.mh.user.service.ProjectInfoService; @@ -51,8 +52,9 @@ public class ProjectInfoServiceImpl implements ProjectInfoService { @Override public void update(ProjectInfoEntity projectInfoEntity) { if (null != projectInfoEntity.getFile()) { - String fileToBase64 = FileUtils.reSizeImg(projectInfoEntity.getFile()); - projectInfoEntity.setPicContent(fileToBase64); + String currentPath = System.getProperty("user.dir"); + FileUtils.copyFile(projectInfoEntity.getFile(), currentPath + Constant.PROJECT_IMG_PATH); + projectInfoEntity.setPicContent("/images/"+projectInfoEntity.getFile().getOriginalFilename()); projectInfoEntity.setPic(projectInfoEntity.getFile().getOriginalFilename()); } projectInfoMapper.updateById(projectInfoEntity); @@ -61,8 +63,12 @@ public class ProjectInfoServiceImpl implements ProjectInfoService { @Override public void save(ProjectInfoEntity projectInfoEntity) { if (null != projectInfoEntity.getFile()) { - String fileToBase64 = FileUtils.reSizeImg(projectInfoEntity.getFile()); - projectInfoEntity.setPicContent(fileToBase64); +// String fileToBase64 = FileUtils.reSizeImg(projectInfoEntity.getFile()); +// projectInfoEntity.setPicContent(fileToBase64); +// projectInfoEntity.setPic(projectInfoEntity.getFile().getOriginalFilename()); + String currentPath = System.getProperty("user.dir"); + FileUtils.copyFile(projectInfoEntity.getFile(), currentPath + Constant.PROJECT_IMG_PATH); + projectInfoEntity.setPicContent("/image/"+projectInfoEntity.getFile().getOriginalFilename()); projectInfoEntity.setPic(projectInfoEntity.getFile().getOriginalFilename()); } projectInfoEntity.setCreateTime(new Date()); diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml index b841e42..8883e2d 100644 --- a/user-service/src/main/resources/application.yml +++ b/user-service/src/main/resources/application.yml @@ -5,6 +5,8 @@ spring: multipart: max-file-size: 50MB max-request-size: 50MB + resources: + static-locations: "classpath:/static/images/,classpath:/static/,classpath:/images/" server: undertow: max-http-post-size: 20MB \ No newline at end of file