From 16b961c2210fe29fd494ac1f9d830dd93503961f Mon Sep 17 00:00:00 2001 From: feiyu02 <risaku@163.com> Date: 星期五, 02 八月 2024 17:25:17 +0800 Subject: [PATCH] 1. 新增监测设备的管理功能 --- src/main/kotlin/cn/flightfeather/supervision/common/utils/FileUtil.kt | 73 ++++++++++++++++++++++++++++++++---- 1 files changed, 64 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..dc7edd0 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() @@ -116,4 +126,49 @@ } } + + fun saveFiles(files: Array<MultipartFile>, basePath: String, path: String): List<String> { + val pathList = mutableListOf<String>() + files.forEach { file -> + val ext = file.resource.file.extension + 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 + } + + /** + * 鎸夌収鍥哄畾瀹藉害鍘嬬缉鍥剧墖鑷砨ase64褰㈠紡 + */ +// fun compressImage2(bytes: ByteArray): String { +// val length = 1440//鍥剧墖闀胯竟鍥哄畾鍘嬬缉涓�1440鍍忕礌 +// +// val input = ByteArrayInputStream(bytes) +// val srcImg = ImageIO.read(input) +// var srcLong = 0 +// var srcShort = 0 +// if (srcImg.width <= srcImg.height) { +// srcLong = srcImg.height +// srcShort = srcImg.width +// +// } else { +// +// } +// val scale = w.toFloat() / srcW +// val h = (srcImg.height * scale).toInt() +// +// val buffImg = BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB) +// buffImg.graphics.drawImage(srcImg.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null) +// val out = ByteArrayOutputStream() +// ImageIO.write(buffImg, "PNG", out) +// +// return SCHEME_PNG + Base64.getEncoder().encodeToString(out.toByteArray()) +// } } \ No newline at end of file -- Gitblit v1.9.3