|
|
|
|
@ -61,20 +61,24 @@ public class Market extends Spider { |
|
|
|
|
Utils.notify("正在下載..."); |
|
|
|
|
Response response = OkHttp.newCall(url); |
|
|
|
|
File file = FileUtil.getCacheFile(Uri.parse(url).getLastPathSegment()); |
|
|
|
|
download(file, response.body().byteStream()); |
|
|
|
|
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1"))); |
|
|
|
|
FileUtil.openFile(FileUtil.chmod(file)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
Utils.notify(e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void download(File file, InputStream is) throws Exception { |
|
|
|
|
private void download(File file, InputStream is, double length) throws Exception { |
|
|
|
|
FileOutputStream os = new FileOutputStream(file); |
|
|
|
|
try (BufferedInputStream input = new BufferedInputStream(is)) { |
|
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
|
int readBytes; |
|
|
|
|
long totalBytes = 0; |
|
|
|
|
while ((readBytes = input.read(buffer)) != -1) { |
|
|
|
|
totalBytes += readBytes; |
|
|
|
|
os.write(buffer, 0, readBytes); |
|
|
|
|
int progress = (int) (totalBytes / length * 100.0); |
|
|
|
|
if (progress % 10 < 5) Utils.notify("正在下載..." + progress + "%"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|