From 301536241fd179abf68044244a97d2d4797b9cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=B8=E7=86=9F=E5=90=B4=E5=BD=A6=E7=A5=96?= Date: Wed, 8 Jul 2026 15:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=A9=E6=96=99=E5=92=8C=E7=89=A9=E6=96=99?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E4=B8=8A=E4=BC=A0SOP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/SrmAuditWorkOrderServiceImpl.java | 42 ++++--- .../oss/service/impl/SysOssServiceImpl.java | 119 +++++++++++++----- 2 files changed, 110 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/xujie/modules/audit/service/impl/SrmAuditWorkOrderServiceImpl.java b/src/main/java/com/xujie/modules/audit/service/impl/SrmAuditWorkOrderServiceImpl.java index 02f41f1..b49d3a7 100644 --- a/src/main/java/com/xujie/modules/audit/service/impl/SrmAuditWorkOrderServiceImpl.java +++ b/src/main/java/com/xujie/modules/audit/service/impl/SrmAuditWorkOrderServiceImpl.java @@ -335,21 +335,18 @@ public class SrmAuditWorkOrderServiceImpl extends ServiceImpl impl // 上传文件 Date date = new Date(); for (MultipartFile file : files) { + SysOssEntity uploadEntity = new SysOssEntity(); String fileName = file.getOriginalFilename(); if (StringUtils.isEmpty(fileName)) { throw new IllegalArgumentException("文件名不能为空"); @@ -103,20 +104,29 @@ public class SysOssServiceImpl extends ServiceImpl impl fileDir.mkdirs(); } // 保存文件 - File targetFile = new File(path, fileName); + File targetFile = new File(path, newFileName); try { file.transferTo(targetFile); } catch (IOException e) { throw new RuntimeException("上传文件失败"); } // 保存文件信息 - oss.setFileName(fileName); - oss.setUrl(path + "\\" + fileName); - oss.setNewFileName(newFileName); - oss.setCreateDate(date); - oss.setFileType(suffix); - oss.setOrderReftype("询价文档"); - this.save(oss); + uploadEntity.setFileName(fileName); + uploadEntity.setUrl(targetFile.getPath()); + uploadEntity.setNewFileName(newFileName); + uploadEntity.setCreateDate(date); + uploadEntity.setFileType(suffix); + uploadEntity.setOrderReftype("询价文档"); + uploadEntity.setCreatedBy(oss.getCreatedBy()); + uploadEntity.setOrderRef1(oss.getOrderRef1()); + uploadEntity.setOrderRef2(oss.getOrderRef2()); + uploadEntity.setOrderRef3(oss.getOrderRef3()); + uploadEntity.setOrderRef4(oss.getOrderRef4()); + uploadEntity.setOrderRef5(oss.getOrderRef5()); + uploadEntity.setOrderRef6(oss.getOrderRef6()); + uploadEntity.setFileTypeCode(oss.getFileTypeCode()); + uploadEntity.setCAdditionalInfo(oss.getCAdditionalInfo()); + this.save(uploadEntity); } } @Override @@ -125,6 +135,7 @@ public class SysOssServiceImpl extends ServiceImpl impl // 上传文件 Date date = new Date(); for (MultipartFile file : files) { + SysOssEntity uploadEntity = new SysOssEntity(); String fileName = file.getOriginalFilename(); if (StringUtils.isEmpty(fileName)) { throw new IllegalArgumentException("文件名不能为空"); @@ -146,20 +157,29 @@ public class SysOssServiceImpl extends ServiceImpl impl fileDir.mkdirs(); } // 保存文件 - File targetFile = new File(path, fileName); + File targetFile = new File(path, newFileName); try { file.transferTo(targetFile); } catch (IOException e) { throw new RuntimeException("上传文件失败"); } // 保存文件信息 - oss.setFileName(fileName); - oss.setUrl(path + "\\" + fileName); - oss.setNewFileName(newFileName); - oss.setCreateDate(date); - oss.setFileType(suffix); - oss.setOrderReftype(oss.getOrderReftype()); - this.save(oss); + uploadEntity.setFileName(fileName); + uploadEntity.setUrl(targetFile.getPath()); + uploadEntity.setNewFileName(newFileName); + uploadEntity.setCreateDate(date); + uploadEntity.setFileType(suffix); + uploadEntity.setOrderReftype(oss.getOrderReftype()); + uploadEntity.setCreatedBy(oss.getCreatedBy()); + uploadEntity.setOrderRef1(oss.getOrderRef1()); + uploadEntity.setOrderRef2(oss.getOrderRef2()); + uploadEntity.setOrderRef3(oss.getOrderRef3()); + uploadEntity.setOrderRef4(oss.getOrderRef4()); + uploadEntity.setOrderRef5(oss.getOrderRef5()); + uploadEntity.setOrderRef6(oss.getOrderRef6()); + uploadEntity.setFileTypeCode(oss.getFileTypeCode()); + uploadEntity.setCAdditionalInfo(oss.getCAdditionalInfo()); + this.save(uploadEntity); } } @Override @@ -206,7 +226,7 @@ public class SysOssServiceImpl extends ServiceImpl impl fileDir.mkdirs(); } // 保存文件 - File targetFile = new File(path, fileName); + File targetFile = new File(path, newFileName); try { file.transferTo(targetFile); } catch (IOException e) { @@ -215,7 +235,7 @@ public class SysOssServiceImpl extends ServiceImpl impl oss.setCreatedBy(oss2.getCreatedBy()); // 保存文件信息 oss.setFileName(fileName); - oss.setUrl(path + "\\" + fileName); + oss.setUrl(targetFile.getPath()); oss.setNewFileName(newFileName); oss.setCreateDate(date); oss.setFileType(suffix); @@ -282,7 +302,7 @@ public class SysOssServiceImpl extends ServiceImpl impl public void removeOssFile(List ids) { List list = lambdaQuery().in(SysOssEntity::getId, ids).list(); for (SysOssEntity entity : list) { - File file = new File(entity.getUrl()); + File file = resolveStorageFile(entity); if (file.exists()) { file.delete(); } @@ -315,7 +335,7 @@ public class SysOssServiceImpl extends ServiceImpl impl } // 创建文件对象 - File file = new File(oss.getUrl()); + File file = resolveStorageFile(oss); if (!file.exists()) { throw new IllegalArgumentException("文件不存在"); } @@ -328,7 +348,7 @@ public class SysOssServiceImpl extends ServiceImpl impl } // 设置响应头 - if ("txt".equals(oss.getFileSuffix())){ + if ("txt".equalsIgnoreCase(resolveFileSuffix(oss))){ response.setContentType("text/plain;charset=utf-8"); }else { response.setContentType("application/octet-stream;charset=utf-8"); @@ -384,21 +404,18 @@ public class SysOssServiceImpl extends ServiceImpl impl if (getFileData.isEmpty()) { throw new RuntimeException("该文件不存在,请刷新列表"); } - File file = new File(getFileData.get(0).getUrl()); + SysOssEntity ossEntity = getFileData.get(0); + File file = resolveStorageFile(ossEntity); + if (!file.exists()) { + throw new RuntimeException("文件不存在,请刷新后重试"); + } //读取缓存1kb byte[] buffer = new byte[1024]; - FileInputStream fis = null; - BufferedInputStream bis = null; - //FileOutputStream fos = null; - //BufferedOutputStream bos = null; response.setContentType("application/force-download;charset=utf-8"); - response.setHeader("Content-disposition", "attachment; filename=" + new String(getFileData.get(0).getFileName().getBytes("gbk"), "iso8859-1")); - try { + response.setHeader("Content-disposition", "attachment; filename=" + new String(ossEntity.getFileName().getBytes("gbk"), "iso8859-1")); + try (FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis)) { //读取文件 - fis = new FileInputStream(file); - //加缓存 - bis = new BufferedInputStream(fis); - //开始读取 int i = bis.read(buffer); //设置输出流 ServletOutputStream os = response.getOutputStream(); @@ -407,10 +424,44 @@ public class SysOssServiceImpl extends ServiceImpl impl os.write(buffer, 0, i); i = bis.read(buffer); } - fis.close(); - //fos.close(); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } + + private File resolveStorageFile(SysOssEntity ossEntity) { + if (ossEntity == null || StringUtils.isEmpty(ossEntity.getUrl())) { + return new File(""); + } + File fileByUrl = new File(ossEntity.getUrl()); + if (StringUtils.isNotEmpty(ossEntity.getNewFileName())) { + File fileByNewName; + if (fileByUrl.isDirectory()) { + fileByNewName = new File(fileByUrl, ossEntity.getNewFileName()); + } else { + File parent = fileByUrl.getParentFile(); + fileByNewName = parent == null ? new File(ossEntity.getNewFileName()) : new File(parent, ossEntity.getNewFileName()); + } + if (fileByNewName.exists()) { + return fileByNewName; + } + } + return fileByUrl; + } + + private String resolveFileSuffix(SysOssEntity ossEntity) { + if (ossEntity == null) { + return ""; + } + if (StringUtils.isNotEmpty(ossEntity.getFileSuffix())) { + return ossEntity.getFileSuffix(); + } + if (StringUtils.isNotEmpty(ossEntity.getFileType())) { + return ossEntity.getFileType(); + } + if (StringUtils.isNotEmpty(ossEntity.getFileName()) && ossEntity.getFileName().contains(".")) { + return ossEntity.getFileName().substring(ossEntity.getFileName().lastIndexOf(".") + 1); + } + return ""; + } }