1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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())
//    }
}