| | |
| | | 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.* |
| | | |
| | | |
| | | object FileUtil { |
| | |
| | | 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() |
| | |
| | | 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 |
| | | } |
| | | } |