In case someone needs to download an image from a website (or camera) and save it as a file, I’ll post the below code as I was facing some obstacles (couldn’t get it done by sendHttpGetRequest, etc).
Obviously, you could use executeCommandline in combination with wget or curl, but I try to keep my scripts as platformindependent as possible, thus trying to avoid executeCommandline.
If someone has a different approach, I am happy if you post it here.
var url = "http://mycamera/record/current.jpg";
var path = "/etc/openhab/html/download";
var file = path + "/" + time.LocalDateTime.now() + ".jpg";
var HttpUtil = Java.type("org.openhab.core.io.net.http.HttpUtil");
var Files = Java.type("java.nio.file.Files");
var Paths = Java.type("java.nio.file.Paths");
var rawImage = HttpUtil.downloadImage(url, 4000);
if (rawImage !== null && rawImage !== undefined) {
var byteArray = rawImage.getBytes();
Files.write(Paths.get(file), byteArray);
}