I bet you already solved the problem, but since this question is about the best google results when searching for "htmlunit download", here is a standard solution. downloadLink- this is an element with a link to the file that you are going to download (button, input, anchor ...)
try {
InputStream is = downloadLink.click().getWebResponse().getContentAsStream();
try {
File f = new File("filename.extension");
OutputStream os = new FileOutputStream(f);
byte[] bytes = new byte[1024];
while (read = is.read(bytes)) {
os.write(bytes, 0, read);
}
os.close();
is.close();
} catch (IOException ex) {
}
} catch (IOException ex) {
}
source
share