简体   繁体   中英

HtmlUnit download file

I am trying to download file via HTMLUnit. But every time when I use code like this:

InputStream stream  = anchor.click().getWebResponse().getContentAsStream();

i get website instead file.

Download link looks like this:

<a id="j_id272:exportToExcel" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['j_id272'],'j_id272:exportToExcel,j_id272:exportToExcel','');}return false" href="#">
     <img id="j_id272:exportToExcelImage" alt="???exportToExcel???" src="/ECRM/gfx/icon_excel.gif">
</a>

Is there any way to download this?

Page page = anchor.click();
BinaryPage bp = new BinaryPage(page.getWebResponse(),page.getEnclosingWindow());
InputStream input = bp.getInputStream();

What is inside your page? Maybe it is a 40X or other 50X error page?

如果你想获得页面,请参阅下面:

  final HtmlPage enclosedPage = (HtmlPage) anchor.click().getEnclosingWindow().getEnclosedPage();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM