管灌系统巡查员智能手机App
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
package cc.shinichi.library.tool.common
 
import java.io.*
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.net.URLDecoder
 
/**
 * HttpURLConnection 下载图片
 */
object HttpUtil {
 
    /**
     * @param urlPath     下载路径
     * @param downloadDir 下载存放目录
     * @return 返回下载文件
     */
    fun downloadFile(urlPath: String?, fileFullName: String, downloadDir: String): File? {
        var file: File? = null
        try {
            // 统一资源
            val url = URL(urlPath)
            // 连接类的父类,抽象类
            val urlConnection = url.openConnection()
            // http的连接类
            val httpURLConnection = urlConnection as HttpURLConnection
            // 设定请求的方法,默认是GET
            httpURLConnection.requestMethod = "GET"
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8")
            // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
            httpURLConnection.connect()
            val bin = BufferedInputStream(httpURLConnection.inputStream)
            val path = downloadDir + File.separatorChar + fileFullName
            file = File(path)
            file.parentFile?.let {
                if (!it.exists()) {
                    it.mkdirs()
                }
            }
            val out: OutputStream = FileOutputStream(file)
            var size = 0
            var len = 0
            val buf = ByteArray(1024)
            while (bin.read(buf).also { size = it } != -1) {
                len += size
                out.write(buf, 0, size)
            }
            bin.close()
            out.close()
            return file
        } catch (e: MalformedURLException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
 
    fun decode(text: String): String {
        return URLDecoder.decode(text)
    }
}