Browse Source

1、优化项目管理图片,改成本地保存。

dev
mh 4 months ago
parent
commit
5ee2a303d0
  1. 20
      common/src/main/java/com/mh/common/utils/FileUtils.java
  2. 3
      user-service/src/main/java/com/mh/user/constants/Constant.java
  3. 5
      user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java
  4. 14
      user-service/src/main/java/com/mh/user/service/impl/ProjectInfoServiceImpl.java
  5. 2
      user-service/src/main/resources/application.yml

20
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();
}
}
}

3
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\\";
}

5
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);
}

14
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());

2
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
Loading…
Cancel
Save