2012-05-19 3 views

답변

9

HtmlUnit을 사용하는 경우 HtmlPage가 있어야합니다. 거기 당신은 HtmlImage를 얻고 파일이 방법을 저장할 수 있습니다

HtmlImage image = page.<HtmlImage>getFirstByXPath("//img[@src='blah']"); 
File imageFile = new File("/path/to/file.jpg"); 
image.saveAs(imageFile); 

를 당신이 URL이있는 경우 ... 이미지를 다운로드 HtmlUnit과 필요 그때는 생각하지 않습니다. 여기

+0

무엇 이미지가 동적으로 HTML 페이지에 삽입합니다. 예를 들어, 서블릿과 함께? 은 입니다. http://example.com/servlet/GetImage&key=1234 –

+1

이 프로세스는 이미지가 서블릿 출력에서 ​​생성 된 결과 인 HtmlPage에있는 한 동일하게 유지됩니다. –

0

내가 이런 식으로 코드를 작성하는 방법입니다

NodeList nlx = downloadPage.getElementsByTagName("a"); 
for (int y = 0; y<nlx.getLength(); y++) { 
    String ss = nlx.item(y).toString(); 
    if(ss.contains("download/?fileformat=kml")) { 
     System.out.println(ss); 
     HtmlElement anchorAttachment = (HtmlElement)nlx.item(y); 
     InputStream is =anchorAttachment.click().getWebResponse().getContentAsStream(); 
     try { 
      //System.out.println(is); 
      OutputStream out = new FileOutputStream(new File(fileName+".KML")); 

      int read=0; 
      byte[] bytes = new byte[1024]; 
      while((read = is.read(bytes))!= -1) { 
       out.write(bytes, 0, read); 
      } 
      is.close(); 
      out.flush(); 
      out.close();  
      System.out.println("New file created!"); 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 
관련 문제