[英]Download a non html file with HtmlUnit
我正在編寫一個JUnit測試,涉及從Web應用程序下載文件。 我如何使用HtmlUnit做到這一點?
我不是什么樣的文件,但是這個測試的代碼可能會有所幫助。 如果沒有嘗試在其他測試中找到答案。
我敢打賭你已經解決了這個問題,但是因為這個問題是google搜索“htmlunit download”時的最佳結果,這里是標准解決方案。 downloadLink
是帶有您要下載的文件的鏈接的元素(按鈕,輸入,錨點......)
try {
InputStream is = downloadLink.click().getWebResponse().getContentAsStream();
try {
File f = new File("filename.extension");
OutputStream os = new FileOutputStream(f);
byte[] bytes = new byte[1024]; // make it bigger if you want. Some recommend 8x, others 100x
while (read = is.read(bytes)) {
os.write(bytes, 0, read);
}
os.close();
is.close();
} catch (IOException ex) {
// Exception handling
}
} catch (IOException ex) {
// Exception handling
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.