Last active
May 5, 2024 07:47
-
-
Save No-Eul/264f0c217c646aca1ac6db35229a3577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Jsoup = org.jsoup.Jsoup, | |
FileOutputStream = java.io.FileOutputStream, | |
Byte = java.lang.Byte, | |
JavaArray = java.lang.reflect.Array; | |
// ... | |
function downloadFile(url, file) { | |
let inputStream, outputStream; | |
try { | |
let execute = Jsoup.connect(url) | |
.ignoreHttpErrors(true) | |
.ignoreContentType(true) | |
.execute(); | |
inputStream = execute.bodyStream(); | |
outputStream = new FileOutputStream(file); | |
let buf = JavaArray.newInstance(Byte.TYPE, 4096), reads; | |
while ((reads = inputStream.read(buf)) > 0) | |
outputStream.write(buf, 0, reads); | |
} catch (e) { | |
Log.e(e.getMessage()); | |
} finally { | |
try { | |
if (!inputStream) inputStream.close(); | |
if (!outputStream) outputStream.close(); | |
} catch (e) { | |
Log.e(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment