From 508103b872b18f2bd6046192d3bcbb99fd51e0d0 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 10 Dec 2024 11:06:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20try-with-resou?= =?UTF-8?q?rces=20=E7=A1=AE=E4=BF=9D=E6=B5=81=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/unknown/MsJmeterElement.java | 147 ++++---- .../metersphere/commons/utils/FileUtils.java | 326 +++++++++++------- 2 files changed, 285 insertions(+), 188 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java index d878270e39..283801c630 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java @@ -23,6 +23,7 @@ import org.apache.jorphan.collections.HashTree; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; @@ -39,71 +40,93 @@ public class MsJmeterElement extends MsTestElement { @Override public void toHashTree(HashTree tree, List hashTree, MsParameter msParameter) { - try { - ParameterConfig config = (ParameterConfig) msParameter; - // 非导出操作,且不是启用状态则跳过执行 - if (!config.isOperating() && !this.isEnable()) { + + ParameterConfig config = (ParameterConfig) msParameter; + + // 非导出操作,且不是启用状态则直接返回 + if (!config.isOperating() && !this.isEnable()) { + return; + } + + try (InputStream inputSource = getStrToStream(jmeterElement)) { + if (inputSource == null) { return; } - InputStream inputSource = getStrToStream(jmeterElement); - if (inputSource != null) { - Object scriptWrapper = SaveService.loadElement(inputSource); - HashTree elementTree = tree; - this.setElementType(scriptWrapper.getClass().getName()); - if (scriptWrapper instanceof TestElement) { - ((TestElement) scriptWrapper).setName(this.getName()); - ((TestElement) scriptWrapper).setEnabled(this.isEnable()); + + Object scriptWrapper = SaveService.loadElement(inputSource); + if (scriptWrapper == null) { + return; + } + + HashTree elementTree = tree; + this.setElementType(scriptWrapper.getClass().getName()); + + if (scriptWrapper instanceof TestElement testElement) { + testElement.setName(this.getName()); + testElement.setEnabled(this.isEnable()); + } + + // CSV 检查与处理 + handleCSVDataSet(config, scriptWrapper); + + // 取出导入的测试计划中的变量 + if (scriptWrapper instanceof TestPlan testPlan) { + if (testPlan.getArguments() != null && StringUtils.isNotEmpty(testPlan.getArguments().getName())) { + elementTree.add(testPlan.getArguments()); } - // csv 检查处理 - if (!config.isOperating() && scriptWrapper instanceof CSVDataSet && ((CSVDataSet) scriptWrapper).isEnabled()) { - String path = ((CSVDataSet) scriptWrapper).getPropertyAsString(ElementConstants.FILENAME); - if (!new File(path).exists()) { - // 检查场景变量中的csv文件是否存在 - String pathArr[] = path.split("\\/"); - String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]); - if (StringUtils.isNotEmpty(csvPath)) { - ((CSVDataSet) scriptWrapper).setProperty(ElementConstants.FILENAME, csvPath); - } else { - MSException.throwException(StringUtils.isEmpty(((CSVDataSet) scriptWrapper).getName()) ? "CSVDataSet" : ((CSVDataSet) scriptWrapper).getName() + ":[ CSV文件不存在 ]"); - } - } - String csvPath = ((CSVDataSet) scriptWrapper).getPropertyAsString(ElementConstants.FILENAME); - if (config.getCsvFilePaths().contains(csvPath)) { - return; - } else { - config.getCsvFilePaths().add(csvPath); - } - } - // 取出导入的测试计划中变量 - if (scriptWrapper instanceof TestPlan) { - TestPlan testPlan = (TestPlan) scriptWrapper; - if (testPlan.getArguments() != null && StringUtils.isNotEmpty(testPlan.getArguments().getName())) { - elementTree.add(testPlan.getArguments()); - } - } - if (config.isOperating()) { - elementTree = tree.add(scriptWrapper); - } else if (!(scriptWrapper instanceof TestPlan) && !(scriptWrapper instanceof ThreadGroup)) { - elementTree = tree.add(scriptWrapper); - } - if (!config.isOperating() && scriptWrapper instanceof ThreadGroup && !((ThreadGroup) scriptWrapper).isEnabled()) { - LogUtil.info(((ThreadGroup) scriptWrapper).getName() + "是被禁用线程组不加入执行"); - } else { - if (CollectionUtils.isNotEmpty(hashTree)) { - for (MsTestElement el : hashTree) { - // 给所有孩子加一个父亲标志 - el.setParent(this); - el.toHashTree(elementTree, el.getHashTree(), config); - } - } + } + + // 添加到 HashTree + if (config.isOperating()) { + elementTree = tree.add(scriptWrapper); + } else if (!(scriptWrapper instanceof TestPlan) && !(scriptWrapper instanceof ThreadGroup)) { + elementTree = tree.add(scriptWrapper); + } + + // 忽略被禁用的线程组 + if (!config.isOperating() && scriptWrapper instanceof ThreadGroup threadGroup && !threadGroup.isEnabled()) { + LogUtil.info(threadGroup.getName() + " 是被禁用线程组,不加入执行"); + return; + } + + // 递归处理子元素 + if (CollectionUtils.isNotEmpty(hashTree)) { + for (MsTestElement el : hashTree) { + el.setParent(this); + el.toHashTree(elementTree, el.getHashTree(), config); } } } catch (Exception ex) { - ex.printStackTrace(); + LogUtil.error("Error in toHashTree: " + ex.getMessage(), ex); MSException.throwException(ex.getMessage()); } } + private void handleCSVDataSet(ParameterConfig config, Object scriptWrapper) { + if (!(scriptWrapper instanceof CSVDataSet csvDataSet)) { + return; + } + + String path = csvDataSet.getPropertyAsString(ElementConstants.FILENAME); + if (!new File(path).exists()) { + // 检查场景变量中的 CSV 文件是否存在 + String[] pathArr = path.split("\\/"); + String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]); + if (StringUtils.isNotEmpty(csvPath)) { + csvDataSet.setProperty(ElementConstants.FILENAME, csvPath); + } else { + String name = StringUtils.defaultIfEmpty(csvDataSet.getName(), "CSVDataSet"); + MSException.throwException(name + ":[ CSV文件不存在 ]"); + } + } + + String csvPath = csvDataSet.getPropertyAsString(ElementConstants.FILENAME); + if (!config.getCsvFilePaths().contains(csvPath)) { + config.getCsvFilePaths().add(csvPath); + } + } + + private String getCSVPath(ParameterConfig config, String name) { if (CollectionUtils.isNotEmpty(config.getVariables())) { List list = config.getVariables().stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList()); @@ -112,7 +135,7 @@ public class MsJmeterElement extends MsTestElement { if (CollectionUtils.isNotEmpty(item.getFiles())) { List names = item.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(names) && !names.contains(name) && name.contains("_")) { - String pathArr[] = name.split("_"); + String[] pathArr = name.split("_"); name = pathArr[pathArr.length - 1]; } if (CollectionUtils.isNotEmpty(names) && names.contains(name)) { @@ -129,15 +152,11 @@ public class MsJmeterElement extends MsTestElement { return null; } - public static InputStream getStrToStream(String sInputString) { - if (StringUtils.isNotEmpty(sInputString)) { - try { - ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes()); - return tInputStringStream; - } catch (Exception ex) { - ex.printStackTrace(); - } + public static InputStream getStrToStream(String inputString) { + if (StringUtils.isNotEmpty(inputString)) { + return new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); } return null; } + } diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java index f2dcca36aa..714ba31b87 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java @@ -68,27 +68,45 @@ public class FileUtils { public static void createFile(String filePath, byte[] fileBytes) { File file = new File(filePath); + + // 如果文件已存在,先删除再创建 if (file.exists()) { - file.delete(); - } - try { - File dir = file.getParentFile(); - if (!dir.exists()) { - dir.mkdirs(); + if (!file.delete()) { + LogUtil.warn("Failed to delete existing file: " + filePath); + return; // 文件删除失败,退出方法 } - file.createNewFile(); - } catch (Exception e) { - LogUtil.error(e); } - try (InputStream in = new ByteArrayInputStream(fileBytes); OutputStream out = new FileOutputStream(file)) { + try { + File dir = file.getParentFile(); + // 确保目录存在 + if (!dir.exists() && !dir.mkdirs()) { + LogUtil.error("Failed to create directory: " + dir.getAbsolutePath()); + return; // 创建目录失败,退出方法 + } + + // 创建新文件 + if (!file.createNewFile()) { + LogUtil.error("Failed to create file: " + filePath); + return; // 文件创建失败,退出方法 + } + } catch (IOException e) { + LogUtil.error("Error during file creation: " + e.getMessage(), e); + return; // 捕获异常并退出方法 + } + + try (InputStream in = new ByteArrayInputStream(fileBytes); + OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + + // 写入数据 final int MAX = 4096; byte[] buf = new byte[MAX]; - for (int bytesRead = in.read(buf, 0, MAX); bytesRead != -1; bytesRead = in.read(buf, 0, MAX)) { + int bytesRead; + while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } } catch (IOException e) { - LogUtil.error(e); + LogUtil.error("Error during file write: " + e.getMessage(), e); MSException.throwException(Translator.get("upload_fail")); } } @@ -98,24 +116,39 @@ public class FileUtils { if (StringUtils.isNotEmpty(path)) { filePath = path; } + if (CollectionUtils.isNotEmpty(bodyUploadIds) && CollectionUtils.isNotEmpty(bodyFiles)) { File testDir = new File(filePath); - if (!testDir.exists()) { - testDir.mkdirs(); + // 如果目标目录不存在,则创建 + if (!testDir.exists() && !testDir.mkdirs()) { + MSException.throwException(Translator.get("create_directory_fail")); } + for (int i = 0; i < bodyUploadIds.size(); i++) { MultipartFile item = bodyFiles.get(i); - validateFileName(item.getOriginalFilename()); - File file = new File(filePath + File.separator + bodyUploadIds.get(i) + "_" + item.getOriginalFilename()); - try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) { - file.createNewFile(); - final int MAX = 4096; + String originalFilename = item.getOriginalFilename(); + validateFileName(originalFilename); // 文件名验证 + + // 拼接完整的文件路径 + File file = new File(filePath + File.separator + bodyUploadIds.get(i) + "_" + originalFilename); + + // 如果文件已经存在,则跳过创建 + if (file.exists()) { + LogUtil.warn("File already exists: " + file.getAbsolutePath()); + continue; + } + + try (InputStream in = item.getInputStream(); + OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + // 读取文件并写入 + final int MAX = 4096; // 设置读取缓冲区大小 byte[] buf = new byte[MAX]; - for (int bytesRead = in.read(buf, 0, MAX); bytesRead != -1; bytesRead = in.read(buf, 0, MAX)) { + int bytesRead; + while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } } catch (IOException e) { - LogUtil.error(e); + LogUtil.error("Error writing file: " + e.getMessage(), e); MSException.throwException(Translator.get("upload_fail")); } } @@ -124,26 +157,45 @@ public class FileUtils { public static String create(String id, MultipartFile item) { String filePath = BODY_FILE_DIR + "/plugin"; + if (item != null) { validateFileName(item.getOriginalFilename()); + + // 确保目标目录存在 File testDir = new File(filePath); - if (!testDir.exists()) { - testDir.mkdirs(); + if (!testDir.exists() && !testDir.mkdirs()) { + LogUtil.error("Failed to create directory: " + filePath); + return null; // 目录创建失败,返回 null } + + // 构造文件路径 File file = new File(filePath + File.separator + id + "_" + item.getOriginalFilename()); - try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) { - file.createNewFile(); + + try (InputStream in = item.getInputStream(); + OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + + // 确保文件被创建 + if (!file.createNewFile() && !file.exists()) { + LogUtil.error("Failed to create file: " + file.getAbsolutePath()); + return null; // 文件创建失败,返回 null + } + final int MAX = 4096; byte[] buf = new byte[MAX]; - for (int bytesRead = in.read(buf, 0, MAX); bytesRead != -1; bytesRead = in.read(buf, 0, MAX)) { + int bytesRead; + while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } } catch (IOException e) { - LogUtil.error(e); + LogUtil.error("Error while processing the file upload: " + e.getMessage(), e); MSException.throwException(Translator.get("upload_fail")); + return null; // 返回 null,表示上传失败 } - return file.getPath(); + + // 返回文件的绝对路径 + return file.getAbsolutePath(); } + return null; } @@ -151,20 +203,32 @@ public class FileUtils { if (CollectionUtils.isNotEmpty(bodyFiles) && StringUtils.isNotBlank(requestId)) { String path = BODY_FILE_DIR + File.separator + requestId; File testDir = new File(path); + // 创建目录,如果目录不存在 if (!testDir.exists()) { - testDir.mkdirs(); + boolean dirCreated = testDir.mkdirs(); + if (!dirCreated) { + LogUtil.error("Failed to create directory: " + path); + MSException.throwException("Directory creation failed"); + } } + bodyFiles.forEach(item -> { validateFileName(item.getOriginalFilename()); File file = new File(path + File.separator + item.getOriginalFilename()); try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) { - file.createNewFile(); - FileUtil.copyStream(in, out); + if (file.createNewFile()) { + FileUtil.copyStream(in, out); // 复制文件内容 + } else { + LogUtil.error("File already exists: " + file.getAbsolutePath()); + MSException.throwException("File already exists"); + } } catch (IOException e) { - LogUtil.error(e); + LogUtil.error("Error uploading file: " + item.getOriginalFilename(), e); MSException.throwException(Translator.get("upload_fail")); } }); + } else { + MSException.throwException("Invalid request ID or body files are empty"); } } @@ -197,24 +261,31 @@ public class FileUtils { * @param targetPath 目标文件夹路径 */ public static void copyFolder(String sourcePath, String targetPath) { - //源文件夹路径 File sourceFile = new File(sourcePath); - //目标文件夹路径 File targetFile = new File(targetPath); + // 检查源文件夹是否存在且是目录 if (!sourceFile.exists() || !sourceFile.isDirectory()) { return; } + + // 如果目标文件夹不存在,则创建目标文件夹 if (!targetFile.exists()) { - targetFile.mkdirs(); + boolean dirCreated = targetFile.mkdirs(); + if (!dirCreated) { + LogUtil.error("Failed to create target directory: " + targetPath); + return; + } } + // 获取源文件夹中的文件和目录 File[] files = sourceFile.listFiles(); - if (files == null || files.length == 0) { + if (files == null) { return; } for (File file : files) { + // 复制文件 copyFileToDir(file, targetFile); } } @@ -292,19 +363,38 @@ public class FileUtils { public static String createFile(MultipartFile bodyFile) { validateFileName(bodyFile.getOriginalFilename()); - String dir = "/opt/metersphere/data/body/tmp/"; + + // 使用 File.separator 以提高跨平台兼容性 + String dir = "/opt/metersphere/data/body/tmp" + File.separator; File fileDir = new File(dir); + + // 检查并创建文件夹 if (!fileDir.exists()) { - fileDir.mkdirs(); + boolean dirCreated = fileDir.mkdirs(); + if (!dirCreated) { + LogUtil.error("Failed to create directory: " + dir); + MSException.throwException("Directory creation failed"); + } } - File file = new File(dir + UUID.randomUUID().toString() + "_" + bodyFile.getOriginalFilename()); + + // 创建目标文件 + String fileName = UUID.randomUUID().toString() + "_" + bodyFile.getOriginalFilename(); + File file = new File(dir + fileName); + try (InputStream in = bodyFile.getInputStream(); OutputStream out = new FileOutputStream(file)) { - file.createNewFile(); + // 创建新文件并复制流 + boolean fileCreated = file.createNewFile(); + if (!fileCreated) { + LogUtil.error("Failed to create file: " + file.getAbsolutePath()); + MSException.throwException("File creation failed"); + } FileUtil.copyStream(in, out); } catch (IOException e) { - LogUtil.error(e); - MSException.throwException(Translator.get("upload_fail")); + LogUtil.error("Error while creating or writing file: " + file.getAbsolutePath(), e); + MSException.throwException("File upload failed"); } + + // 返回文件路径 return file.getPath(); } @@ -374,23 +464,19 @@ public class FileUtils { public static void getFiles(HashTree tree, List files) { for (Object key : tree.keySet()) { HashTree node = tree.get(key); - if (key instanceof HTTPSamplerProxy) { - HTTPSamplerProxy source = (HTTPSamplerProxy) key; - if (source != null && source.getHTTPFiles().length > 0) { - for (HTTPFileArg arg : source.getHTTPFiles()) { - BodyFile file = new BodyFile(); - file.setId(arg.getParamName()); - file.setName(arg.getPath()); - if (arg.getPropertyAsBoolean("isRef")) { - file.setStorage(StorageConstants.FILE_REF.name()); - file.setFileId(arg.getPropertyAsString("fileId")); - } - files.add(file); + if (key instanceof HTTPSamplerProxy source) { + for (HTTPFileArg arg : source.getHTTPFiles()) { + BodyFile file = new BodyFile(); + file.setId(arg.getParamName()); + file.setName(arg.getPath()); + if (arg.getPropertyAsBoolean("isRef")) { + file.setStorage(StorageConstants.FILE_REF.name()); + file.setFileId(arg.getPropertyAsString("fileId")); } + files.add(file); } - } else if (key instanceof CSVDataSet) { - CSVDataSet source = (CSVDataSet) key; - if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) { + } else if (key instanceof CSVDataSet source) { + if (StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) { BodyFile file = new BodyFile(); file.setId(source.getPropertyAsString("filename")); file.setName(source.getPropertyAsString("filename")); @@ -420,37 +506,33 @@ public class FileUtils { } public static File byteToFile(byte[] buf, String filePath, String fileName) { - BufferedOutputStream bos = null; - FileOutputStream fos = null; - File file = null; + File file; try { + // 确保文件目录存在 File dir = new File(filePath); - if (!dir.exists()) { - dir.mkdirs(); + if (!dir.exists() && !dir.mkdirs()) { + LogUtil.error("Failed to create directory: " + filePath); + return null; // 目录创建失败,返回 null } + + // 创建文件对象 file = new File(filePath + File.separator + fileName); - fos = new FileOutputStream(file); - bos = new BufferedOutputStream(fos); - bos.write(buf); + + // 使用 try-with-resources 自动关闭流 + try (FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(fos)) { + + bos.write(buf); + } catch (IOException e) { + LogUtil.error("Error while writing bytes to file: " + e.getMessage(), e); + return null; // 返回 null,表示写入文件失败 + } } catch (Exception e) { - e.printStackTrace(); - } finally { - if (bos != null) { - try { - bos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + LogUtil.error("Error occurred while creating file: " + e.getMessage(), e); + return null; // 返回 null,表示其他异常 } - return file; + + return file; // 返回生成的文件 } public static String fileToStr(File tradeFile) { @@ -463,7 +545,7 @@ public class FileUtils { bos.write(b, 0, n); } buffer = bos.toString(); - } catch (Exception e) { + } catch (Exception ignored) { } return buffer; } @@ -473,23 +555,19 @@ public class FileUtils { List list = new ArrayList<>(); for (Object key : tree.keySet()) { HashTree node = tree.get(key); - if (key instanceof HTTPSamplerProxy) { - HTTPSamplerProxy source = (HTTPSamplerProxy) key; - if (source != null && source.getHTTPFiles().length > 0) { - for (HTTPFileArg arg : source.getHTTPFiles()) { - if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) { - FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId")); - if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) { - list.add(fileMetadata); - arg.setPath(fileMetadata.getName()); - arg.setName(fileMetadata.getName()); - } + if (key instanceof HTTPSamplerProxy source) { + for (HTTPFileArg arg : source.getHTTPFiles()) { + if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) { + FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId")); + if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) { + list.add(fileMetadata); + arg.setPath(fileMetadata.getName()); + arg.setName(fileMetadata.getName()); } } } - } else if (key instanceof CSVDataSet) { - CSVDataSet source = (CSVDataSet) key; - if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) { + } else if (key instanceof CSVDataSet source) { + if (StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) { if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) { FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId")); if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) { @@ -548,7 +626,7 @@ public class FileUtils { try { File file = CompressUtils.zipFiles(UUID.randomUUID().toString() + ".zip", files); FileSystemResource resource = new FileSystemResource(file); - byte[] fileByte = this.fileToByte(file); + byte[] fileByte = fileToByte(file); if (fileByte != null) { ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { @Override @@ -569,6 +647,7 @@ public class FileUtils { List jarFiles = new LinkedList<>(); // jar 包 JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class); + assert jarConfigService != null; List jars = jarConfigService.list(); jars.forEach(jarConfig -> { try { @@ -578,7 +657,7 @@ public class FileUtils { file = new File(path + File.separator); } FileSystemResource resource = new FileSystemResource(file); - byte[] fileByte = this.fileToByte(file); + byte[] fileByte = fileToByte(file); if (fileByte != null) { ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { @Override @@ -601,20 +680,27 @@ public class FileUtils { // 获取附件 List files = new LinkedList<>(); getFiles(hashTree, files); + if (CollectionUtils.isNotEmpty(files)) { for (BodyFile bodyFile : files) { File file = new File(bodyFile.getName()); - if (file != null && !file.exists()) { - FileSystemResource resource = new FileSystemResource(file); - byte[] fileByte = this.fileToByte(file); - if (fileByte != null) { - ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { - @Override - public String getFilename() throws IllegalStateException { - return resource.getFilename(); - } - }; - multipartFiles.add(byteArrayResource); + // 如果文件不存在,则不处理 + if (file.exists()) { + try { + byte[] fileByte = fileToByte(file); + if (fileByte != null) { + // 使用 ByteArrayResource 包装文件字节数据 + ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { + @Override + public String getFilename() throws IllegalStateException { + return file.getName(); // 使用 file.getName() 获取文件名 + } + }; + multipartFiles.add(byteArrayResource); + } + } catch (Exception e) { + LogUtil.error("Error reading file: " + file.getName(), e); + // 处理异常时可以考虑继续处理其他文件,或者抛出异常 } } } @@ -622,21 +708,13 @@ public class FileUtils { return multipartFiles; } - public static Boolean writeToFile(String filePath, byte[] content) { - OutputStream oStream = null; - try { - oStream = new FileOutputStream(filePath); + public static boolean writeToFile(String filePath, byte[] content) { + try (OutputStream oStream = new FileOutputStream(filePath)) { oStream.write(content); - return Boolean.TRUE; - } catch (Exception exception) { - exception.printStackTrace(); - return Boolean.FALSE; - } finally { - try { - oStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } + return true; + } catch (IOException e) { + LogUtil.error("Error writing to file: " + filePath, e); + return false; } }