| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,79 @@ | |||
| 1 | + package info.xiaomo.core.untils | ||
| 2 | + | ||
| 3 | + import java.io.File | ||
| 4 | + import java.io.FileOutputStream | ||
| 5 | + import java.net.URL | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * @author : xiaomo (https://xiaomo.info) (https://github.com/xiaomoinfo) | ||
| 9 | + * @created : 2016/12/26 13:25 | ||
| 10 | + */ | ||
| 11 | + object DownUtil { | ||
| 12 | + | ||
| 13 | + @Throws(Exception::class) | ||
| 14 | + fun download(urlString: String) { | ||
| 15 | + val file = File(urlString) | ||
| 16 | + val filename = file.name | ||
| 17 | + // 构造URL | ||
| 18 | + val url = URL(urlString) | ||
| 19 | + // 打开连接 | ||
| 20 | + val con = url.openConnection() | ||
| 21 | + // 输入流 | ||
| 22 | + val `is` = con.getInputStream() | ||
| 23 | + // 1K的数据缓冲 | ||
| 24 | + val bs = ByteArray(1024) | ||
| 25 | + // 读取到的数据长度 | ||
| 26 | + val len = 0 | ||
| 27 | + // 输出的文件流 | ||
| 28 | + val os = FileOutputStream(filename) | ||
| 29 | + // 开始读取 | ||
| 30 | + while ((`is`.read(bs)) != -1) { | ||
| 31 | + os.write(bs, 0, len) | ||
| 32 | + } | ||
| 33 | + // 完毕,关闭所有链接 | ||
| 34 | + os.close() | ||
| 35 | + `is`.close() | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 下载图片 | ||
| 41 | + * | ||
| 42 | + * @param urlString url | ||
| 43 | + * @param filePath 存储路径 D:\MIR\config\data\ | ||
| 44 | + */ | ||
| 45 | + @Throws(Exception::class) | ||
| 46 | + fun download(urlString: String, filePath: String) { | ||
| 47 | + val file = File(urlString) | ||
| 48 | + val filename = file.name | ||
| 49 | + // 构造URL | ||
| 50 | + val url = URL(urlString) | ||
| 51 | + // 打开连接 | ||
| 52 | + val con = url.openConnection() | ||
| 53 | + // 输入流 | ||
| 54 | + val `is` = con.getInputStream() | ||
| 55 | + // 1K的数据缓冲 | ||
| 56 | + val bs = ByteArray(1024) | ||
| 57 | + // 读取到的数据长度 | ||
| 58 | + var len = 0 | ||
| 59 | + // 输出的文件流 | ||
| 60 | + | ||
| 61 | + val output = File(filePath) | ||
| 62 | + if (!output.exists()) { | ||
| 63 | + val res = output.mkdir() | ||
| 64 | + if (res) { | ||
| 65 | + println("目录创建成功" + filePath) | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + val os = FileOutputStream(filePath + filename) | ||
| 70 | + // 开始读取 | ||
| 71 | + while ((`is`.read(bs)) != -1) { | ||
| 72 | + os.write(bs, 0, len) | ||
| 73 | + } | ||
| 74 | + // 完毕,关闭所有链接 | ||
| 75 | + os.close() | ||
| 76 | + `is`.close() | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments