【问题标题】:How to Download a file placed in a image button - HTMLUNIT如何下载放置在图像按钮中的文件 - HTMLUNIT
【发布时间】:2015-10-08 16:47:55
【问题描述】:

通过单击图像按钮下载文件。

我有这个link

我一直在尝试下载文件(两个保存图标图像按钮)。当我单击图像时,它会提示下载 zip 文件。

这是在页面的 VIEWSOURCE 按钮中看到的标签

<input type="image" name="ctl00$m$g_b265ad4d_cd49_41f3_a9f2_0090f0aa5504$ctl00$gvBidSetsFile$ctl02$ImageButton1" id="ctl00_m_g_b265ad4d_cd49_41f3_a9f2_0090f0aa5504_ctl00_gvBidSetsFile_ctl02_ImageButton1" title="Download" src="/SiteAssets/images/saveitem.gif" style="height:18px;width:18px;border-width:0px;">

    WebClient client = new WebClient(BrowserVersion.FIREFOX_38);
    HtmlPage homePage = null;
   // Document doc = null;  
    String base="https://bidset.nycsca.org/SitePages/Obtain%20Solicitation.aspx?SN=16-15323D-1&ReqType=Solicitation&IsDlg=1&IsDlg=1";

    try {
        client.getOptions().setUseInsecureSSL(true);
    client.setAjaxController(new NicelyResynchronizingAjaxController());
    client.waitForBackgroundJavaScript(1000);
    client.waitForBackgroundJavaScriptStartingBefore(1000);
    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
    client.getOptions().setThrowExceptionOnScriptError(false);

    homePage = client.getPage(base);
    synchronized (homePage) {
        homePage.wait(5000);
    }
    System.out.println("file Page : " + homePage.getUrl());
   // Document dd = Jsoup.parse(homePage.asXml());
    HtmlInput docs= homePage.getFirstByXPath("//input[@id='ctl00_m_g_b265ad4d_cd49_41f3_a9f2_0090f0aa5504_ctl00_gvBidSetsFile_ctl02_ImageButton1']");

        homePage = bidDocs.click();

问题: 当我得到 ​​p>

HtmlInput docs =  homePage.getFirstByXPath
 ("//input[@id='ctl00_m_g_b265ad4d_cd49_41f3_a9f2_0090f0aa5504_ctl00_gvBidSetsFile_ctl02_ImageButton1']");

表演可以吗,

homePage = bidDocs.click();

在这里点击保存图标下载文件。

  1. 我很困惑如何在 HTMLUNIT 的帮助下下载此文件。 我想在我的本地驱动器中下载文件。

  2. (homePage = bidDocs.click();)之后是否可以获取首页的url链接

即店铺链接,String docurl=get homePage's link . ????

  1. 如果我能得到链接,我可以使用BufferedStream 下载文件。

        File file = new File("C:/TRY/file/abc.zip");
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(docUrl).openStream());
            fout = new FileOutputStream(file);
    
            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
    

我必须在这里使用HTMLUNIT,因为它是javascriptiframe 站点。 感谢您的帮助。

非常感谢。

【问题讨论】:

    标签: javascript java download jsoup htmlunit


    【解决方案1】:

    选择文件标签

     HtmlInput download = pageCheck.getByXPath("//input[@title='Download']");
    

    用于流式传输

     download.click().getWebResponse().getContentAsStream()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2014-09-27
      • 2011-02-07
      • 1970-01-01
      • 2022-11-21
      相关资源
      最近更新 更多