3
我在写一个JUnit测试,涉及从Web应用程序中下载文件。我如何用HtmlUnit做到这一点?使用HtmlUnit下载非html文件
我在写一个JUnit测试,涉及从Web应用程序中下载文件。我如何用HtmlUnit做到这一点?使用HtmlUnit下载非html文件
我不是什么类型的文件,但可能代码为this测试可能会有所帮助。如果不尝试在其他测试中找到答案。
我敢打赌你已经解决了这个问题,但是因为这个问题是谷歌搜索“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
}
请你看问题的http://stackoverflow.com/questions/2131049 /问题在-的HtmlUnit-API换Java的无头的浏览器 – 2010-01-25 08:38:02