diff --git a/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java b/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java index e487ed22..7c926c77 100644 --- a/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java @@ -55,7 +55,7 @@ public class PqIcdPathServiceImpl extends ServiceImpl { PqIcdPath.FileVO fileVO = new PqIcdPath.FileVO(); fileVO.setUrl(commInstallPath + "\\DeviceControl\\Config\\" + pqIcdPath.getName() + ".txt"); - fileVO.setName(pqIcdPath.getName()+ ".txt"); + fileVO.setName(pqIcdPath.getName() + ".txt"); pqIcdPath.setMappingFile(fileVO); }); return page; @@ -74,53 +74,62 @@ public class PqIcdPathServiceImpl extends ServiceImpl FILE_SIZE_LIMIT) { -// throw new BusinessException(IcdResponseEnum.FILE_SIZE_ERROR); -// } -// -// try { -// // 如果文件存在,则先删除 -// String mappingFilePath = commInstallPath + "\\DeviceControl\\Config" + mappingFilename; -// Path path = Paths.get(mappingFilePath); -// File file = path.toFile(); -// if (file.exists()) { -// file.delete(); -// } -// -// // 保存文件 -// byte[] bytes = mappingFile.getBytes(); -// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mappingFilePath)); -// bufferedOutputStream.write(bytes); -// bufferedOutputStream.flush(); -// -// bufferedOutputStream.close(); -// } catch (IOException e) { -// throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED); -// } -// } else { -// throw new BusinessException(IcdResponseEnum.FILE_NOT_NULL); -// } + if (ObjectUtil.isNotNull(mappingFile) && !mappingFile.isEmpty()) { + String mappingFilename = mappingFile.getOriginalFilename(); + if (!mappingFilename.endsWith(".txt")) { + throw new BusinessException(IcdResponseEnum.FILE_TYPE_ERROR); + } + if (mappingFile.getSize() > FILE_SIZE_LIMIT) { + throw new BusinessException(IcdResponseEnum.FILE_SIZE_ERROR); + } + + try { + // 如果文件存在,则先删除 + String mappingFilePath = commInstallPath + "\\DeviceControl\\Config\\" + mappingFilename; + Path path = Paths.get(mappingFilePath); + File file = path.toFile(); + if (file.exists()) { + file.delete(); + } + + // 保存文件 + byte[] bytes = mappingFile.getBytes(); + BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mappingFilePath)); + bufferedOutputStream.write(bytes); + bufferedOutputStream.flush(); + + bufferedOutputStream.close(); + } catch (IOException e) { + throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED); + } + } else { + throw new BusinessException(IcdResponseEnum.FILE_NOT_NULL); + } + + this.executeRestartCmd(commInstallPath); + + return this.save(pqIcdPath); + } + + /** + * 执行重启通讯服务脚本 + * + * @param commInstallPath + */ + private void executeRestartCmd(String commInstallPath) { // 以管理员身份运行bat脚本 - String batFilePath = commInstallPath + "\\重启通讯服务.bat"; + String batFilePath = commInstallPath + "\\重启所有服务.bat"; try { Runtime.getRuntime().exec(batFilePath); } catch (IOException e) { log.error("重启通讯服务失败", e); } - - return this.save(pqIcdPath); } private String getCommInstallPath() { -// String workDir = System.getProperty("user.dir"); - String workDir = "D:\\program\\CN_Gather"; + String workDir = System.getProperty("user.dir"); +// String workDir = "D:\\program\\CN_Gather"; // 获取映射文件存放文件夹 String dirPath = workDir.substring(0, workDir.lastIndexOf("\\")) + "\\9100"; return dirPath; @@ -149,7 +158,7 @@ public class PqIcdPathServiceImpl extends ServiceImpl