From 76dc2e2a8ad8a26ecbc459c051380c6fc67060ec Mon Sep 17 00:00:00 2001 From: dimengzhe Date: Thu, 23 Jan 2025 11:00:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/FileBatchUploadComponent.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/FileBatchUploadComponent.java b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/FileBatchUploadComponent.java index bb31f12..b304f99 100644 --- a/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/FileBatchUploadComponent.java +++ b/yxt-common/yxt-common-base/src/main/java/com/yxt/common/base/config/component/FileBatchUploadComponent.java @@ -14,8 +14,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -116,14 +115,26 @@ public class FileBatchUploadComponent { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } - try { + /* try { file.transferTo(destFile); } catch (IOException e) { e.printStackTrace(); } // 返回文件路劲 String fullUrl = urlPrefix + filePath; - list.add(fullUrl); + list.add(fullUrl);*/ + try (InputStream inputStream = file.getInputStream(); + BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destFile))) { + byte[] buffer = new byte[4096]; // 使用更大的缓冲区,4KB + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + list.add(urlPrefix + filePath); // 上传完成后加入返回结果列表 + } catch (IOException e) { + e.printStackTrace(); + return rm.setMsg("文件上传失败"); + } } else { return rm.setMsg("上传文件格式不正确"); }