From b99e425766ac0e393cf4b47e7e4b989c7c424ced Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期二, 18 三月 2025 16:49:24 +0800 Subject: [PATCH] art: 设备附件管理,工艺参数,精度参数初始代码添加 --- lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 46 insertions(+), 5 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..92334c4 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 @@ -46,7 +46,7 @@ if (fileNameNonSuffix == null) return null; // 鑾峰彇鏂囦欢鍔犲瘑鍚嶇О 1 淇濊瘉鏂囦欢鍞竴 涓嶅瓨鍦ㄨ鐩栭棶棰� 2 鐩綍涓嬩繚瀛樻枃浠跺姞瀵嗗悕绉� 鍘婚櫎鏂囦欢鍏抽敭淇℃伅 - String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()); + String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix; Long fileSize = file.getSize(); boolean b = uploadFile(file, absolutePath, encodeFileName); if (!b) @@ -54,7 +54,7 @@ FileUploadResult dto = new FileUploadResult(); dto.setFileName(fileNameNonSuffix); dto.setFileEncodeName(encodeFileName); - dto.setFilePath(relativePath); + dto.setFilePath(relativePath + encodeFileName); dto.setFileSize(fileSize); dto.setFileSuffix(suffix); return dto; @@ -67,9 +67,7 @@ public static void deleteFile(String filePath, String fileName) { String absolutePath = fileUploadFolder + "/" + filePath + "/"; File targetFile = new File(absolutePath, fileName); - if (null != targetFile && targetFile.isFile()) { - targetFile.delete(); - } + targetFile.deleteOnExit(); } public static void downLoadFile(HttpServletResponse response, String fileName, String filePath, String toFileName) { @@ -115,6 +113,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