refactor: 使用 try-with-resources 确保流关闭

This commit is contained in:
fit2-zhao 2024-12-10 11:06:30 +08:00 committed by Craftsman
parent 01c9a4dca0
commit 508103b872
2 changed files with 285 additions and 188 deletions

View File

@ -23,6 +23,7 @@ import org.apache.jorphan.collections.HashTree;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -39,71 +40,93 @@ public class MsJmeterElement extends MsTestElement {
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter msParameter) {
try {
ParameterConfig config = (ParameterConfig) msParameter; ParameterConfig config = (ParameterConfig) msParameter;
// 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) { // 非导出操作且不是启用状态则直接返回
if (!config.isOperating() && !this.isEnable()) {
return;
}
try (InputStream inputSource = getStrToStream(jmeterElement)) {
if (inputSource == null) {
return; return;
} }
InputStream inputSource = getStrToStream(jmeterElement);
if (inputSource != null) { Object scriptWrapper = SaveService.loadElement(inputSource);
Object scriptWrapper = SaveService.loadElement(inputSource); if (scriptWrapper == null) {
HashTree elementTree = tree; return;
this.setElementType(scriptWrapper.getClass().getName()); }
if (scriptWrapper instanceof TestElement) {
((TestElement) scriptWrapper).setName(this.getName()); HashTree elementTree = tree;
((TestElement) scriptWrapper).setEnabled(this.isEnable()); 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); // 添加到 HashTree
if (!new File(path).exists()) { if (config.isOperating()) {
// 检查场景变量中的csv文件是否存在 elementTree = tree.add(scriptWrapper);
String pathArr[] = path.split("\\/"); } else if (!(scriptWrapper instanceof TestPlan) && !(scriptWrapper instanceof ThreadGroup)) {
String csvPath = this.getCSVPath(config, pathArr[pathArr.length - 1]); elementTree = tree.add(scriptWrapper);
if (StringUtils.isNotEmpty(csvPath)) { }
((CSVDataSet) scriptWrapper).setProperty(ElementConstants.FILENAME, csvPath);
} else { // 忽略被禁用的线程组
MSException.throwException(StringUtils.isEmpty(((CSVDataSet) scriptWrapper).getName()) ? "CSVDataSet" : ((CSVDataSet) scriptWrapper).getName() + "[ CSV文件不存在 ]"); if (!config.isOperating() && scriptWrapper instanceof ThreadGroup threadGroup && !threadGroup.isEnabled()) {
} LogUtil.info(threadGroup.getName() + " 是被禁用线程组,不加入执行");
} return;
String csvPath = ((CSVDataSet) scriptWrapper).getPropertyAsString(ElementConstants.FILENAME); }
if (config.getCsvFilePaths().contains(csvPath)) {
return; // 递归处理子元素
} else { if (CollectionUtils.isNotEmpty(hashTree)) {
config.getCsvFilePaths().add(csvPath); for (MsTestElement el : hashTree) {
} el.setParent(this);
} el.toHashTree(elementTree, el.getHashTree(), config);
// 取出导入的测试计划中变量
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);
}
}
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); LogUtil.error("Error in toHashTree: " + ex.getMessage(), ex);
MSException.throwException(ex.getMessage()); 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) { private String getCSVPath(ParameterConfig config, String name) {
if (CollectionUtils.isNotEmpty(config.getVariables())) { if (CollectionUtils.isNotEmpty(config.getVariables())) {
List<ScenarioVariable> list = config.getVariables().stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList()); List<ScenarioVariable> list = config.getVariables().stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList());
@ -112,7 +135,7 @@ public class MsJmeterElement extends MsTestElement {
if (CollectionUtils.isNotEmpty(item.getFiles())) { if (CollectionUtils.isNotEmpty(item.getFiles())) {
List<String> names = item.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()); List<String> names = item.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(names) && !names.contains(name) && name.contains("_")) { if (CollectionUtils.isNotEmpty(names) && !names.contains(name) && name.contains("_")) {
String pathArr[] = name.split("_"); String[] pathArr = name.split("_");
name = pathArr[pathArr.length - 1]; name = pathArr[pathArr.length - 1];
} }
if (CollectionUtils.isNotEmpty(names) && names.contains(name)) { if (CollectionUtils.isNotEmpty(names) && names.contains(name)) {
@ -129,15 +152,11 @@ public class MsJmeterElement extends MsTestElement {
return null; return null;
} }
public static InputStream getStrToStream(String sInputString) { public static InputStream getStrToStream(String inputString) {
if (StringUtils.isNotEmpty(sInputString)) { if (StringUtils.isNotEmpty(inputString)) {
try { return new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
} catch (Exception ex) {
ex.printStackTrace();
}
} }
return null; return null;
} }
} }

View File

@ -68,27 +68,45 @@ public class FileUtils {
public static void createFile(String filePath, byte[] fileBytes) { public static void createFile(String filePath, byte[] fileBytes) {
File file = new File(filePath); File file = new File(filePath);
// 如果文件已存在先删除再创建
if (file.exists()) { if (file.exists()) {
file.delete(); if (!file.delete()) {
} LogUtil.warn("Failed to delete existing file: " + filePath);
try { return; // 文件删除失败退出方法
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
} }
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; final int MAX = 4096;
byte[] buf = new byte[MAX]; 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); out.write(buf, 0, bytesRead);
} }
} catch (IOException e) { } catch (IOException e) {
LogUtil.error(e); LogUtil.error("Error during file write: " + e.getMessage(), e);
MSException.throwException(Translator.get("upload_fail")); MSException.throwException(Translator.get("upload_fail"));
} }
} }
@ -98,24 +116,39 @@ public class FileUtils {
if (StringUtils.isNotEmpty(path)) { if (StringUtils.isNotEmpty(path)) {
filePath = path; filePath = path;
} }
if (CollectionUtils.isNotEmpty(bodyUploadIds) && CollectionUtils.isNotEmpty(bodyFiles)) { if (CollectionUtils.isNotEmpty(bodyUploadIds) && CollectionUtils.isNotEmpty(bodyFiles)) {
File testDir = new File(filePath); 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++) { for (int i = 0; i < bodyUploadIds.size(); i++) {
MultipartFile item = bodyFiles.get(i); MultipartFile item = bodyFiles.get(i);
validateFileName(item.getOriginalFilename()); String originalFilename = item.getOriginalFilename();
File file = new File(filePath + File.separator + bodyUploadIds.get(i) + "_" + item.getOriginalFilename()); validateFileName(originalFilename); // 文件名验证
try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) {
file.createNewFile(); // 拼接完整的文件路径
final int MAX = 4096; 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]; 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); out.write(buf, 0, bytesRead);
} }
} catch (IOException e) { } catch (IOException e) {
LogUtil.error(e); LogUtil.error("Error writing file: " + e.getMessage(), e);
MSException.throwException(Translator.get("upload_fail")); MSException.throwException(Translator.get("upload_fail"));
} }
} }
@ -124,26 +157,45 @@ public class FileUtils {
public static String create(String id, MultipartFile item) { public static String create(String id, MultipartFile item) {
String filePath = BODY_FILE_DIR + "/plugin"; String filePath = BODY_FILE_DIR + "/plugin";
if (item != null) { if (item != null) {
validateFileName(item.getOriginalFilename()); validateFileName(item.getOriginalFilename());
// 确保目标目录存在
File testDir = new File(filePath); File testDir = new File(filePath);
if (!testDir.exists()) { if (!testDir.exists() && !testDir.mkdirs()) {
testDir.mkdirs(); LogUtil.error("Failed to create directory: " + filePath);
return null; // 目录创建失败返回 null
} }
// 构造文件路径
File file = new File(filePath + File.separator + id + "_" + item.getOriginalFilename()); 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; final int MAX = 4096;
byte[] buf = new byte[MAX]; 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); out.write(buf, 0, bytesRead);
} }
} catch (IOException e) { } catch (IOException e) {
LogUtil.error(e); LogUtil.error("Error while processing the file upload: " + e.getMessage(), e);
MSException.throwException(Translator.get("upload_fail")); MSException.throwException(Translator.get("upload_fail"));
return null; // 返回 null表示上传失败
} }
return file.getPath();
// 返回文件的绝对路径
return file.getAbsolutePath();
} }
return null; return null;
} }
@ -151,20 +203,32 @@ public class FileUtils {
if (CollectionUtils.isNotEmpty(bodyFiles) && StringUtils.isNotBlank(requestId)) { if (CollectionUtils.isNotEmpty(bodyFiles) && StringUtils.isNotBlank(requestId)) {
String path = BODY_FILE_DIR + File.separator + requestId; String path = BODY_FILE_DIR + File.separator + requestId;
File testDir = new File(path); File testDir = new File(path);
// 创建目录如果目录不存在
if (!testDir.exists()) { 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 -> { bodyFiles.forEach(item -> {
validateFileName(item.getOriginalFilename()); validateFileName(item.getOriginalFilename());
File file = new File(path + File.separator + item.getOriginalFilename()); File file = new File(path + File.separator + item.getOriginalFilename());
try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) { try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) {
file.createNewFile(); if (file.createNewFile()) {
FileUtil.copyStream(in, out); FileUtil.copyStream(in, out); // 复制文件内容
} else {
LogUtil.error("File already exists: " + file.getAbsolutePath());
MSException.throwException("File already exists");
}
} catch (IOException e) { } catch (IOException e) {
LogUtil.error(e); LogUtil.error("Error uploading file: " + item.getOriginalFilename(), e);
MSException.throwException(Translator.get("upload_fail")); 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 目标文件夹路径 * @param targetPath 目标文件夹路径
*/ */
public static void copyFolder(String sourcePath, String targetPath) { public static void copyFolder(String sourcePath, String targetPath) {
//源文件夹路径
File sourceFile = new File(sourcePath); File sourceFile = new File(sourcePath);
//目标文件夹路径
File targetFile = new File(targetPath); File targetFile = new File(targetPath);
// 检查源文件夹是否存在且是目录
if (!sourceFile.exists() || !sourceFile.isDirectory()) { if (!sourceFile.exists() || !sourceFile.isDirectory()) {
return; return;
} }
// 如果目标文件夹不存在则创建目标文件夹
if (!targetFile.exists()) { if (!targetFile.exists()) {
targetFile.mkdirs(); boolean dirCreated = targetFile.mkdirs();
if (!dirCreated) {
LogUtil.error("Failed to create target directory: " + targetPath);
return;
}
} }
// 获取源文件夹中的文件和目录
File[] files = sourceFile.listFiles(); File[] files = sourceFile.listFiles();
if (files == null || files.length == 0) { if (files == null) {
return; return;
} }
for (File file : files) { for (File file : files) {
// 复制文件
copyFileToDir(file, targetFile); copyFileToDir(file, targetFile);
} }
} }
@ -292,19 +363,38 @@ public class FileUtils {
public static String createFile(MultipartFile bodyFile) { public static String createFile(MultipartFile bodyFile) {
validateFileName(bodyFile.getOriginalFilename()); 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); File fileDir = new File(dir);
// 检查并创建文件夹
if (!fileDir.exists()) { 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)) { 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); FileUtil.copyStream(in, out);
} catch (IOException e) { } catch (IOException e) {
LogUtil.error(e); LogUtil.error("Error while creating or writing file: " + file.getAbsolutePath(), e);
MSException.throwException(Translator.get("upload_fail")); MSException.throwException("File upload failed");
} }
// 返回文件路径
return file.getPath(); return file.getPath();
} }
@ -374,23 +464,19 @@ public class FileUtils {
public static void getFiles(HashTree tree, List<BodyFile> files) { public static void getFiles(HashTree tree, List<BodyFile> files) {
for (Object key : tree.keySet()) { for (Object key : tree.keySet()) {
HashTree node = tree.get(key); HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) { if (key instanceof HTTPSamplerProxy source) {
HTTPSamplerProxy source = (HTTPSamplerProxy) key; for (HTTPFileArg arg : source.getHTTPFiles()) {
if (source != null && source.getHTTPFiles().length > 0) { BodyFile file = new BodyFile();
for (HTTPFileArg arg : source.getHTTPFiles()) { file.setId(arg.getParamName());
BodyFile file = new BodyFile(); file.setName(arg.getPath());
file.setId(arg.getParamName()); if (arg.getPropertyAsBoolean("isRef")) {
file.setName(arg.getPath()); file.setStorage(StorageConstants.FILE_REF.name());
if (arg.getPropertyAsBoolean("isRef")) { file.setFileId(arg.getPropertyAsString("fileId"));
file.setStorage(StorageConstants.FILE_REF.name());
file.setFileId(arg.getPropertyAsString("fileId"));
}
files.add(file);
} }
files.add(file);
} }
} else if (key instanceof CSVDataSet) { } else if (key instanceof CSVDataSet source) {
CSVDataSet source = (CSVDataSet) key; if (StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
BodyFile file = new BodyFile(); BodyFile file = new BodyFile();
file.setId(source.getPropertyAsString("filename")); file.setId(source.getPropertyAsString("filename"));
file.setName(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) { public static File byteToFile(byte[] buf, String filePath, String fileName) {
BufferedOutputStream bos = null; File file;
FileOutputStream fos = null;
File file = null;
try { try {
// 确保文件目录存在
File dir = new File(filePath); File dir = new File(filePath);
if (!dir.exists()) { if (!dir.exists() && !dir.mkdirs()) {
dir.mkdirs(); LogUtil.error("Failed to create directory: " + filePath);
return null; // 目录创建失败返回 null
} }
// 创建文件对象
file = new File(filePath + File.separator + fileName); file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos); // 使用 try-with-resources 自动关闭流
bos.write(buf); 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) { } catch (Exception e) {
e.printStackTrace(); LogUtil.error("Error occurred while creating file: " + e.getMessage(), e);
} finally { return null; // 返回 null表示其他异常
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return file;
return file; // 返回生成的文件
} }
public static String fileToStr(File tradeFile) { public static String fileToStr(File tradeFile) {
@ -463,7 +545,7 @@ public class FileUtils {
bos.write(b, 0, n); bos.write(b, 0, n);
} }
buffer = bos.toString(); buffer = bos.toString();
} catch (Exception e) { } catch (Exception ignored) {
} }
return buffer; return buffer;
} }
@ -473,23 +555,19 @@ public class FileUtils {
List<FileMetadata> list = new ArrayList<>(); List<FileMetadata> list = new ArrayList<>();
for (Object key : tree.keySet()) { for (Object key : tree.keySet()) {
HashTree node = tree.get(key); HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) { if (key instanceof HTTPSamplerProxy source) {
HTTPSamplerProxy source = (HTTPSamplerProxy) key; for (HTTPFileArg arg : source.getHTTPFiles()) {
if (source != null && source.getHTTPFiles().length > 0) { if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
for (HTTPFileArg arg : source.getHTTPFiles()) { FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId"));
if (arg.getPropertyAsBoolean("isRef") && fileMetadataService != null) { if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(arg.getPropertyAsString("fileId")); list.add(fileMetadata);
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) { arg.setPath(fileMetadata.getName());
list.add(fileMetadata); arg.setName(fileMetadata.getName());
arg.setPath(fileMetadata.getName());
arg.setName(fileMetadata.getName());
}
} }
} }
} }
} else if (key instanceof CSVDataSet) { } else if (key instanceof CSVDataSet source) {
CSVDataSet source = (CSVDataSet) key; if (StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) { if (source.getPropertyAsBoolean("isRef") && fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId")); FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(source.getPropertyAsString("fileId"));
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) { if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
@ -548,7 +626,7 @@ public class FileUtils {
try { try {
File file = CompressUtils.zipFiles(UUID.randomUUID().toString() + ".zip", files); File file = CompressUtils.zipFiles(UUID.randomUUID().toString() + ".zip", files);
FileSystemResource resource = new FileSystemResource(file); FileSystemResource resource = new FileSystemResource(file);
byte[] fileByte = this.fileToByte(file); byte[] fileByte = fileToByte(file);
if (fileByte != null) { if (fileByte != null) {
ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) {
@Override @Override
@ -569,6 +647,7 @@ public class FileUtils {
List<Object> jarFiles = new LinkedList<>(); List<Object> jarFiles = new LinkedList<>();
// jar // jar
JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class); JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class);
assert jarConfigService != null;
List<JarConfig> jars = jarConfigService.list(); List<JarConfig> jars = jarConfigService.list();
jars.forEach(jarConfig -> { jars.forEach(jarConfig -> {
try { try {
@ -578,7 +657,7 @@ public class FileUtils {
file = new File(path + File.separator); file = new File(path + File.separator);
} }
FileSystemResource resource = new FileSystemResource(file); FileSystemResource resource = new FileSystemResource(file);
byte[] fileByte = this.fileToByte(file); byte[] fileByte = fileToByte(file);
if (fileByte != null) { if (fileByte != null) {
ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) {
@Override @Override
@ -601,20 +680,27 @@ public class FileUtils {
// 获取附件 // 获取附件
List<BodyFile> files = new LinkedList<>(); List<BodyFile> files = new LinkedList<>();
getFiles(hashTree, files); getFiles(hashTree, files);
if (CollectionUtils.isNotEmpty(files)) { if (CollectionUtils.isNotEmpty(files)) {
for (BodyFile bodyFile : files) { for (BodyFile bodyFile : files) {
File file = new File(bodyFile.getName()); File file = new File(bodyFile.getName());
if (file != null && !file.exists()) { // 如果文件不存在则不处理
FileSystemResource resource = new FileSystemResource(file); if (file.exists()) {
byte[] fileByte = this.fileToByte(file); try {
if (fileByte != null) { byte[] fileByte = fileToByte(file);
ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { if (fileByte != null) {
@Override // 使用 ByteArrayResource 包装文件字节数据
public String getFilename() throws IllegalStateException { ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) {
return resource.getFilename(); @Override
} public String getFilename() throws IllegalStateException {
}; return file.getName(); // 使用 file.getName() 获取文件名
multipartFiles.add(byteArrayResource); }
};
multipartFiles.add(byteArrayResource);
}
} catch (Exception e) {
LogUtil.error("Error reading file: " + file.getName(), e);
// 处理异常时可以考虑继续处理其他文件或者抛出异常
} }
} }
} }
@ -622,21 +708,13 @@ public class FileUtils {
return multipartFiles; return multipartFiles;
} }
public static Boolean writeToFile(String filePath, byte[] content) { public static boolean writeToFile(String filePath, byte[] content) {
OutputStream oStream = null; try (OutputStream oStream = new FileOutputStream(filePath)) {
try {
oStream = new FileOutputStream(filePath);
oStream.write(content); oStream.write(content);
return Boolean.TRUE; return true;
} catch (Exception exception) { } catch (IOException e) {
exception.printStackTrace(); LogUtil.error("Error writing to file: " + filePath, e);
return Boolean.FALSE; return false;
} finally {
try {
oStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }