Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2314010
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-03-16 11:22:50

。。

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class getHtml2 {
  14. public void getHtmlPicture(String httpUrl) {
  15. URL url;
  16. BufferedInputStream in;
  17. FileOutputStream file;
  18. try {
  19.    System.out.println("取网络图片");
  20.    String fileName = httpUrl.substring(httpUrl.lastIndexOf("/"));
  21.    String filePath = "F:\\FocuSimple\\test\\src\\pic\\";
  22.    url = new URL(httpUrl);
  23.   
  24.    in = new BufferedInputStream(url.openStream());
  25.   
  26.    file = new FileOutputStream(new File(filePath+fileName));
  27.    int t;
  28.    while ((t = in.read()) != -1) {
  29.    file.write(t);
  30.    }
  31.    file.close();
  32.    in.close();
  33.   System.out.println("图片获取成功");
  34. } catch (MalformedURLException e) {
  35.    e.printStackTrace();
  36. } catch (FileNotFoundException e) {
  37.   e.printStackTrace();
  38. } catch (IOException e) {
  39.    e.printStackTrace();
  40. }
  41. }
  42.   
  43. public String getHtmlCode(String httpUrl) throws IOException {
  44. String content ="";
  45. URL uu = new URL(httpUrl); // 创建URL类对象
  46. BufferedReader ii = new BufferedReader(new InputStreamReader(uu
  47.     .openStream())); // //使用openStream得到一输入流并由此构造一个BufferedReader对象
  48. String input;
  49. while ((input = ii.readLine()) != null) { // 建立读取循环,并判断是否有读取值
  50.    content += input;
  51. }
  52. ii.close();
  53. return content;
  54. }
  55.  
  56. public void get(String url) throws IOException {
  57.   
  58. String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
  59. String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
  60.   
  61. String content = this.getHtmlCode(url);
  62. System.out.println(content);
  63.   
  64. Pattern pattern = Pattern.compile(searchImgReg);
  65. Matcher matcher = pattern.matcher(content);
  66. while (matcher.find()) {
  67.    System.out.println(matcher.group(3));
  68.   this.getHtmlPicture(url+matcher.group(3));
  69.      
  70. }
  71.   
  72. pattern = Pattern.compile(searchImgReg2);
  73. matcher = pattern.matcher(content);
  74. while (matcher.find()) {
  75.    System.out.println(matcher.group(3));
  76.   this.getHtmlPicture(matcher.group(3));
  77.       
  78. }
  79. // searchImgReg =
  80. // "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
  81. }
  82. public static void main(String[] args) throws IOException {
  83. String url = "";
  84. getHtml2 gcp = new getHtml2();
  85. gcp.get(url);
  86. }
  87. }

阅读(9293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~