From 85ef942e7195abeb71466b7159c3ee30161e1e54 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 07 四月 2026 08:51:01 +0800
Subject: [PATCH] 2026.4.7
---
src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt | 65 ++++++++++++++++++++++++++++----
1 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt b/src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt
index f82d5f5..f1a87c1 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt
@@ -1,13 +1,12 @@
package cn.flightfeather.supervision.common.utils
-import java.io.File
-import java.io.FileOutputStream
-import java.io.FileInputStream
+import cn.flightfeather.supervision.common.exception.BizException
+import org.springframework.web.multipart.MultipartFile
+import java.io.*
+import java.util.*
-
-
-class FileUtil {
+object FileUtil {
@Throws(Exception::class)
fun uploadFile(file: ByteArray, filePath: String, fileName: String) {
val targetFile = File(filePath)
@@ -20,13 +19,24 @@
out.close()
}
+ fun delFile(path: String): Boolean {
+ val file = File(path)
+ if (!file.exists()) return false
+ if (file.isDirectory) return false
+ try {
+ file.delete()
+ return true
+ } catch (e: Exception) {
+ throw BizException("鏂囦欢鍒犻櫎鍑洪敊", e.cause)
+ }
+ }
+
//鍒犻櫎鏂囦欢澶�
fun delFolder(folderPath: String) {
try {
delAllFile(folderPath) //鍒犻櫎瀹岄噷闈㈡墍鏈夊唴瀹�
- var filePath = folderPath
- filePath = filePath
- val myFilePath = java.io.File(filePath)
+ val filePath = folderPath
+ val myFilePath = File(filePath)
myFilePath.delete() //鍒犻櫎绌烘枃浠跺す
} catch (e: Exception) {
e.printStackTrace()
@@ -112,8 +122,45 @@
fos.write(b, 0, a)
a = fis.read(b)
}
+
}
}
}
+
+ fun saveFiles(files: Array<MultipartFile>, basePath: String, path: String): List<String> {
+ val pathList = mutableListOf<String>()
+ files.forEach { file ->
+ val ext = file.originalFilename?.split(".")?.last()
+ val fileName = UUID.randomUUID().toString() + "." + ext
+ pathList.add("$path$fileName")
+ val absolutePath = "${basePath}/$path/"
+ try {
+ //璋冪敤鏂囦欢淇濆瓨鏂规硶
+ uploadFile(file.bytes, absolutePath, fileName)
+ } catch (e: Exception) {
+ // TODO: handle exception
+ }
+ }
+ return pathList
+ }
+
+ /**
+ * 鎸夌収璺緞寤虹珛鏂囦欢澶癸紝骞惰嚜鍔ㄥ鐞嗛噸澶嶈矾寰�
+ */
+ fun mkDirs(basePath: String): File {
+ var file = File(basePath)
+ var i = 1
+ while (file.exists() && i <= 100) {
+ val path = "$basePath($i)"
+ file = File(path)
+ i++
+ }
+ if (file.exists()) {
+ delAllFile(file.absolutePath)
+ } else {
+ file.mkdirs()
+ }
+ return file
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3