2014-10-31 18 views
0

htmlunit-2.13을 사용하여 .zip 파일을 다운로드하려고합니다. 파일에서 다운로드 할 수있는 웹에서 .zip 파일 다운로드

: - http://www.bmfbovespa.com.br/consulta-isin/BuscaCodigosIsin.aspx?Idioma=pt-br
-
그리고 링크 '방코 드 Dados Completo'

다음 자바 코딩 - 링크를 클릭
'드 Arquivos 다운로드'를 .zip 파일 대신 html 파일을 저장하고 있습니다.

public class Teste { 

    public static void main(String args[]) 
    { 
     try 
     { 
      LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 
      java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.ALL); 

      HtmlPage page = null; 
      String url = "http://www.bmfbovespa.com.br/consulta-isin/BuscaCodigosIsin.aspx?Idioma=pt-br"; 

      @SuppressWarnings("deprecation") 
      WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_10); 
      webClient.getOptions().setThrowExceptionOnScriptError(false); 
      webClient.getOptions().setJavaScriptEnabled(true); 

      WebRequest webRequest = new WebRequest(new URL(url)); 
      webRequest.setCharset("UTF-8"); 

      page = webClient.getPage(webRequest); 

      HtmlAnchor anchor1 = (HtmlAnchor) page.getElementById("ctl00_contentPlaceHolderConteudo_hplCompleto"); 
      HtmlPage page2 = anchor1.click(); 

      InputStream is = page2.getWebResponse().getContentAsStream(); 
      FileOutputStream output = new FileOutputStream("/tmp/isin.zip"); 

      IOUtils.copy(is, output); 
      output.close(); 

      System.out.println("New file created!");    
     } 
     catch (FailingHttpStatusCodeException e1) 
     { 
      System.out.println("FailingHttpStatusCodeException thrown:" + e1.getMessage()); 
      e1.printStackTrace(); 

     } 
     catch (MalformedURLException e1) 
     { 
      System.out.println("MalformedURLException thrown:" + e1.getMessage()); 
      e1.printStackTrace(); 

     } 
     catch (IOException e1) 
     { 
      System.out.println("IOException thrown:" + e1.getMessage()); 
      e1.printStackTrace(); 

     } 
     catch(Exception e) 
     { 
      System.out.println("General exception thrown:" + e.getMessage()); 
      e.printStackTrace(); 

     } 
    } 

} 

답변

0

이와 비슷한 페이지에서 지퍼를 가져 오려고하면 비슷한 문제가 발생합니다. 이것은 내 해결책이었습니다.

WebClient wc = new WebClient(BrowserVersion.CHROME); 

HtmlPage p = wc.getPage(url) 

// I believe clicking the element returns an UnexpectedPage 
// because the content type is 'application/zip' 

UnexpectedPage up = p.getElementById(buttonId).click(); 

InputStream in = up.getInputStream(); 

... 
관련 문제