From 02ae7138146e6964c4b5875930cbb8896ad840fc Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期二, 01 七月 2025 12:25:59 +0800 Subject: [PATCH] art:设备管理-代码迁移 --- lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 85 insertions(+), 0 deletions(-) diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java index 6473768..eb57299 100644 --- a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java @@ -1,5 +1,7 @@ package org.jeecg.common.util; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.jeecg.common.api.vo.FileUploadResult; import org.jeecg.common.exception.ExceptionCast; import org.springframework.beans.factory.annotation.Value; @@ -9,11 +11,14 @@ import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Date; import java.util.List; @Component +@Slf4j public class FileUtil { private static String fileUploadFolder; @@ -60,6 +65,43 @@ return dto; } + public static FileUploadResult uploadFile(byte[] fileData, String fileName) { + String suffix = getFileSuffix(fileName); + Date currentDate = DateUtils.getDate(); + String monthStr = DateUtils.date2Str(currentDate, DateUtils.monthFormat.get()); + // 鐩稿璺緞 + String relativePath = "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; + // 缁濆璺緞 + String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; + String fileNameNonSuffix = getFilenameNonSuffix(fileName); + if (StringUtils.isBlank(fileNameNonSuffix)) { + return null; + } + // 鑾峰彇鏂囦欢鍔犲瘑鍚嶇О 1 淇濊瘉鏂囦欢鍞竴 涓嶅瓨鍦ㄨ鐩栭棶棰� 2 鐩綍涓嬩繚瀛樻枃浠跺姞瀵嗗悕绉� 鍘婚櫎鏂囦欢鍏抽敭淇℃伅 + String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix; + Long fileSize = (long) fileData.length; + try { + File targetFile = new File(absolutePath, encodeFileName); + if(!targetFile.getParentFile().exists()) { + //鍒涘缓鐩綍 + targetFile.getParentFile().mkdirs(); + } + Files.write(Paths.get(absolutePath + encodeFileName), fileData); + FileUploadResult dto = new FileUploadResult(); + dto.setFileName(fileNameNonSuffix); + dto.setFileEncodeName(encodeFileName); + dto.setFilePath(relativePath + encodeFileName); + dto.setFileSize(fileSize); + dto.setFileSuffix(suffix); + return dto; + } catch (IOException e) { + log.error("鏂囦欢鍐欏叆澶辫触: {}", e.getMessage(), e); + return null; + } + + } + + /** * @param filePath 璺緞 * @param fileName 鏂囦欢鍚� @@ -115,6 +157,49 @@ } } + public static void downLoadFile(HttpServletResponse response, String filePath, String toFileName) { + String absolutePath = fileUploadFolder + filePath; + File file = new File(absolutePath); + if (file.exists()) { + byte[] buffer = new byte[1024]; + FileInputStream fis = null; + BufferedInputStream bis = null; + try { + response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 鍛婅瘔娴忚鍣ㄨ緭鍑哄唴瀹逛负娴� + // response.setHeader("Content-Disposition", "attachment;fileName="+ new + // String(toFileName.getBytes("UTF-8"),"ISO-8859-1")); + response.setHeader("Content-Disposition", + "attachment;fileName=" + URLEncoder.encode(toFileName, "UTF-8")); + fis = new FileInputStream(file); + bis = new BufferedInputStream(fis); + OutputStream os = response.getOutputStream(); + int i = bis.read(buffer); + while (i != -1) { + os.write(buffer, 0, i); + i = bis.read(buffer); + } + os.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + } + } + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + } + } + } + } else { + ExceptionCast.cast("鏂囦欢涓嶅瓨鍦�"); + } + } + /** * 涓婁紶鏂囦欢宸ュ叿绫� * -- Gitblit v1.9.3