package cn.flightfeather.supervision.common.utils
|
|
import java.awt.Image
|
import java.awt.image.BufferedImage
|
import java.io.*
|
import java.util.*
|
import javax.imageio.ImageIO
|
|
|
object FileUtil {
|
@Throws(Exception::class)
|
fun uploadFile(file: ByteArray, filePath: String, fileName: String) {
|
val targetFile = File(filePath)
|
if (!targetFile.exists()) {
|
targetFile.mkdirs()
|
}
|
val out = FileOutputStream(filePath + fileName)
|
out.write(file)
|
out.flush()
|
out.close()
|
}
|
|
//删除文件夹
|
fun delFolder(folderPath: String) {
|
try {
|
delAllFile(folderPath) //删除完里面所有内容
|
var filePath = folderPath
|
filePath = filePath
|
val myFilePath = java.io.File(filePath)
|
myFilePath.delete() //删除空文件夹
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
|
}
|
|
fun delAllFile(path: String): Boolean {
|
var flag = false
|
val file = File(path)
|
if (!file.exists()) {
|
return flag
|
}
|
if (!file.isDirectory) {
|
return flag
|
}
|
val tempList = file.list()
|
var temp: File? = null
|
for (i in tempList!!.indices) {
|
if (path.endsWith(File.separator)) {
|
temp = File(path + tempList[i])
|
} else {
|
temp = File(path + File.separator + tempList[i])
|
}
|
if (temp.isFile) {
|
temp.delete()
|
}
|
if (temp.isDirectory) {
|
delAllFile(path + "/" + tempList[i])//先删除文件夹里面的文件
|
delFolder(path + "/" + tempList[i])//再删除空文件夹
|
flag = true
|
}
|
}
|
return flag
|
}
|
|
@Throws(Exception::class)
|
fun copy(file: File, toFile: File) {
|
val b = ByteArray(1024)
|
var a: Int
|
val fis: FileInputStream
|
val fos: FileOutputStream
|
if (file.isDirectory) {
|
var filepath = file.absolutePath
|
filepath = filepath.replace("\\\\".toRegex(), "/")
|
var toFilepath = toFile.absolutePath
|
toFilepath = toFilepath.replace("\\\\".toRegex(), "/")
|
val lastIndexOf = filepath.lastIndexOf("/")
|
toFilepath += filepath.substring(lastIndexOf, filepath.length)
|
val copy = File(toFilepath)
|
//复制文件夹
|
if (!copy.exists()) {
|
copy.mkdir()
|
}
|
//遍历文件夹
|
for (f in file.listFiles()!!) {
|
copy(f, copy)
|
}
|
} else {
|
if (toFile.isDirectory) {
|
var filepath = file.absolutePath
|
filepath = filepath.replace("\\\\".toRegex(), "/")
|
var toFilepath = toFile.absolutePath
|
toFilepath = toFilepath.replace("\\\\".toRegex(), "/")
|
val lastIndexOf = filepath.lastIndexOf("/")
|
toFilepath += filepath.substring(lastIndexOf, filepath.length)
|
|
//写文件
|
val newFile = File(toFilepath)
|
fis = FileInputStream(file)
|
fos = FileOutputStream(newFile)
|
a = fis.read(b)
|
while (a != -1) {
|
fos.write(b, 0, a)
|
a = fis.read(b)
|
}
|
} else {
|
//写文件
|
fis = FileInputStream(file)
|
fos = FileOutputStream(toFile)
|
a = fis.read(b)
|
while (a != -1) {
|
fos.write(b, 0, a)
|
a = fis.read(b)
|
}
|
}
|
|
}
|
}
|
|
/**
|
* 按照固定宽度压缩图片至base64形式
|
*/
|
// 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())
|
// }
|
}
|