Skip to content

Instantly share code, notes, and snippets.

@No-Eul
Last active May 5, 2024 07:47
Show Gist options
  • Save No-Eul/264f0c217c646aca1ac6db35229a3577 to your computer and use it in GitHub Desktop.
Save No-Eul/264f0c217c646aca1ac6db35229a3577 to your computer and use it in GitHub Desktop.
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