From e5caf12a0cd52158a03a92f3d0d9dee3fe655737 Mon Sep 17 00:00:00 2001 From: dimengzhe Date: Thu, 23 Jan 2025 16:30:17 +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 | 111 +++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) 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 1ae0105..6ad255a 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 @@ -60,7 +60,7 @@ public class FileBatchUploadComponent { * @param relativePath * @return */ - public ResultBean batchUploadFile( MultipartFile[] files, boolean hasDateDir, String relativePath) { + public ResultBean batchUploadFileOld( MultipartFile[] files, boolean hasDateDir, String relativePath) { ResultBean rm = ResultBean.fireFail(); if (files.length > 9) { //最多只能上传9张图片 @@ -187,4 +187,113 @@ public class FileBatchUploadComponent { return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "GB"; } } + + /** + * 压缩图片直到文件大小小于等于目标大小(单位:KB) + * + * @param inputStream 原始图片的输入流 + * @param targetSizeKB 目标文件大小(KB) + * @return 压缩后的字节数组 + * @throws IOException + */ + private byte[] compressImageToTargetSize(InputStream inputStream, int targetSizeKB) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + // 使用 Thumbnailator 压缩图片 + int quality = 100; // 初始质量为100(即不压缩) + Thumbnails.of(inputStream) + .outputQuality(quality / 100.0) // 设置输出质量 + .toOutputStream(outputStream); + + byte[] compressedImage = outputStream.toByteArray(); + while (compressedImage.length / 1024 > targetSizeKB && quality > 10) { // 如果文件大于目标大小,则继续减少质量 + quality -= 5; // 每次减少5的质量 + outputStream.reset(); // 清空输出流 + + Thumbnails.of(inputStream) + .outputQuality(quality / 100.0) + .toOutputStream(outputStream); + + compressedImage = outputStream.toByteArray(); // 获取压缩后的字节数组 + } + + return compressedImage; + } + + /** + * 批量上传 + * @param files + * @param hasDateDir + * @param relativePath + * @return + */ + public ResultBean batchUploadFile( MultipartFile[] files, boolean hasDateDir, String relativePath) { + ResultBean rm = ResultBean.fireFail(); + + if (files.length > 9) { //最多只能上传9张图片 + return rm.setMsg("最多只能上传9张图片"); + } + + int index = 1; + List list = new ArrayList<>(); + for (MultipartFile file: files) { + if (file.getSize() == 0) { + return rm.setMsg("上传文件不能为空"); + } + //文件大小 +// long size = file.getSize(); +// String fileSize = getPrintSize(size); + // 文件名 + String fileName = file.getOriginalFilename(); + // 后缀名 + String suffixName = fileName.substring(fileName.lastIndexOf(".")); + suffixName = suffixName.toLowerCase(); + if (suffixName.equals(".pdf")) { // pdf文件 + ResultBean resultBean = fileUploadComponent.uploadFile(file); + FileUploadResult uploadResult = resultBean.getData(); + String fullUrl = uploadResult.getFullUrl(); + list.add(fullUrl); + } else if(suffixName.equals(".png") || suffixName.equals(".jpg") || suffixName.equals(".jpeg") + || suffixName.equals(".bmp") || suffixName.equals(".gif") || suffixName.equals(".tiff")) { + // 原文件名 + String prefixName = fileName.substring(0,fileName.indexOf(".")); + // 新文件名:文件原名称 + ‘-’ + 生成的时间戳 + String filePath = prefixName + "_" + dateFileName() + index + suffixName; + if (hasDateDir) { + String dateStr = DateUtil.format(new Date(), "yyyyMMdd"); + // 增加日期目录 + filePath = dateStr + "/" + filePath; + } + if (StringUtils.isNotBlank(relativePath)) { + // 增加指定目录 + filePath = relativePath + "/" + filePath; + } + // 上传后的文件含完整路径 + String path = uploadPath + filePath; + // 上传后的文件 + File destFile = new File(path); + if (!destFile.getParentFile().exists()) { + destFile.getParentFile().mkdirs(); + } + try (InputStream inputStream = file.getInputStream()) { + // 获取图片压缩后的字节数组 + byte[] compressedImage = compressImageToTargetSize(inputStream, 100); // 压缩至100KB以内 + + // 将压缩后的字节数组保存为文件 + try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destFile))) { + outputStream.write(compressedImage); // 写入压缩后的字节数组 + list.add(urlPrefix + filePath); // 上传完成后加入返回结果列表 + } + } catch (IOException e) { + e.printStackTrace(); + return rm.setMsg("文件上传失败"); + } + + } else { + return rm.setMsg("上传文件格式不正确"); + } + index++; + } + return rm.success().setData(list); + } }