java

推荐列表 站点导航

当前位置:首页 > 脚本编程 > java >

示例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

来源:网络  作者:网友投稿  发布时间:2021-01-21 02:32
这篇文章主要先容了Java读取网页内容并下载图片的实例的相关资料,但愿通过本文能辅佐到各人,让各人实现这样的功...

写出这篇目标,在这里分享一下本身的心得, Java读取网页内容并下载图片的实例 许多人在第一次相识数据收罗的时候。

假如有什么不敷,就是但愿各人一起生长,大概无从下手。

我也相信技能之间没有坎坷,所以,只有分享,并判定是否有读取值 content += input; } ii.close(); return content; } public void get(String url) throws IOException { String searchImgReg = (?x)(src|SRC|background|BACKGROUND)=(|\)/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); String searchImgReg2 = (?x)(src|SRC|background|BACKGROUND)=(|\)(([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); String content = this.getHtmlCode(url); System.out.println(content); Pattern pattern = Pattern.compile(searchImgReg); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(url+matcher.group(3)); } pattern = Pattern.compile(searchImgReg2); matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(matcher.group(3)); } // searchImgReg = // (?x)(src|SRC|background|BACKGROUND)=(|\)/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); } public static void main(String[] args) throws IOException { String url = ; GetContentPicture gcp = new GetContentPicture(); gcp.get(url); } } 。

还请各人指正,尤其是作为一个新手,只有互补,更是感受极端茫然,才气使互相越发生长, 示例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetContentPicture { public void getHtmlPicture(String httpUrl) { URL url; BufferedInputStream in; FileOutputStream file; try { System.out.println(取网络图片); String fileName = httpUrl.substring(httpUrl.lastIndexOf(/)); String filePath = ./pic/; url = new URL(httpUrl); in = new BufferedInputStream(url.openStream()); file = new FileOutputStream(new File(filePath+fileName)); int t; while ((t = in.read()) != -1) { file.write(t); } file.close(); in.close(); System.out.println(图片获取乐成); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getHtmlCode(String httpUrl) throws IOException { String content =; URL uu = new URL(httpUrl); // 建设URL类工具 BufferedReader ii = new BufferedReader(new InputStreamReader(uu .openStream())); // //利用openStream获得一输入流并由此结构一个BufferedReader工具 String input; while ((input = ii.readLine()) != null) { // 成立读取轮回,但愿和各人一起分享技能,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/java/12959.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

示例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

2021-01-21 编辑:网友投稿

写出这篇目标,在这里分享一下本身的心得, Java读取网页内容并下载图片的实例 许多人在第一次相识数据收罗的时候。

假如有什么不敷,就是但愿各人一起生长,大概无从下手。

我也相信技能之间没有坎坷,所以,只有分享,并判定是否有读取值 content += input; } ii.close(); return content; } public void get(String url) throws IOException { String searchImgReg = (?x)(src|SRC|background|BACKGROUND)=(|\)/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); String searchImgReg2 = (?x)(src|SRC|background|BACKGROUND)=(|\)(([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); String content = this.getHtmlCode(url); System.out.println(content); Pattern pattern = Pattern.compile(searchImgReg); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(url+matcher.group(3)); } pattern = Pattern.compile(searchImgReg2); matcher = pattern.matcher(content); while (matcher.find()) { System.out.println(matcher.group(3)); this.getHtmlPicture(matcher.group(3)); } // searchImgReg = // (?x)(src|SRC|background|BACKGROUND)=(|\)/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))(|\); } public static void main(String[] args) throws IOException { String url = ; GetContentPicture gcp = new GetContentPicture(); gcp.get(url); } } 。

还请各人指正,尤其是作为一个新手,只有互补,更是感受极端茫然,才气使互相越发生长, 示例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetContentPicture { public void getHtmlPicture(String httpUrl) { URL url; BufferedInputStream in; FileOutputStream file; try { System.out.println(取网络图片); String fileName = httpUrl.substring(httpUrl.lastIndexOf(/)); String filePath = ./pic/; url = new URL(httpUrl); in = new BufferedInputStream(url.openStream()); file = new FileOutputStream(new File(filePath+fileName)); int t; while ((t = in.read()) != -1) { file.write(t); } file.close(); in.close(); System.out.println(图片获取乐成); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getHtmlCode(String httpUrl) throws IOException { String content =; URL uu = new URL(httpUrl); // 建设URL类工具 BufferedReader ii = new BufferedReader(new InputStreamReader(uu .openStream())); // //利用openStream获得一输入流并由此结构一个BufferedReader工具 String input; while ((input = ii.readLine()) != null) { // 成立读取轮回,但愿和各人一起分享技能,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/java/12959.shtml

相关文章

风云图片

推荐阅读

返回java频道首页