java 指定路径取图片_java 从网页中获取图片路径与从js中获取图片路径
发布日期:2021-06-24 11:46:34 浏览次数:2 分类:技术文章

本文共 2625 字,大约阅读时间需要 8 分钟。

packagetest;/*** Created by yuantongqin on 2016/12/5.*/importjava.io.*;importjava.net.MalformedURLException;importjava.net.URL;importjava.util.ArrayList;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern;public classgetHtml2 {

public voidgetHtmlPicture(String httpUrl) {

URL url;BufferedInputStream in;FileOutputStream file;try{

System.out.println("取网络图片");String fileName = httpUrl.substring(httpUrl.lastIndexOf("/"));String filePath = "F:\\FocuSimple\\test\\src\\pic\\";url = newURL(httpUrl);in = newBufferedInputStream(url.openStream());file = newFileOutputStream(newFile(filePath+fileName));intt;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();}

}

publicString getHtmlCode(String httpUrl) throwsIOException {

String content ="";URL uu = newURL(httpUrl);//创建URL类对象BufferedReader ii = newBufferedReader(newInputStreamReader(uu

.openStream()));// //使用openStream得到一输入流并由此构造一个BufferedReader对象String input;while((input = ii.readLine()) != null) { //建立读取循环,并判断是否有读取值content += input;}

ii.close();// return "js中内容";returncontent;}

public voidget(String url) throwsIOException {

String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";String searchImgReg2 = "http://[/,+&=\\.\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF|jpeg)";// String searchImgReg2 = "(?x)(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF|jpeg)))";String content = this.getHtmlCode(url);System.out.println(content);Pattern pattern = Pattern.compile(searchImgReg2);Matcher matcher = pattern.matcher(content);// while (matcher.find()) { this.getHtmlPicture(url+matcher.group(3));// String group = matcher.group();// System.out.println(group);// mList.add(group); } pattern = Pattern.compile(searchImgReg2);// matcher = pattern.matcher(content);while(matcher.find()) {

String group = matcher.group();System.out.println(group);mList.add(group);// this.getHtmlPicture(matcher.group(3));}

}

staticList mList= newArrayList();public static voidmain(String[] args) throwsIOException {

// String url = "http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%B8%DF%C7%E5%B1%DA%D6%BD&fr=ala&ala=1&pos=0&alatpl=wallpaper&oriquery=%E9%AB%98%E6%B8%85%E5%A3%81%E7%BA%B8";String url = "http://www.daimg.com/photo/list_4_1.html";getHtml2 gcp = newgetHtml2();gcp.get(url);System.out.println("==长度=="+mList.size());}

}

转载地址:https://blog.csdn.net/weixin_32570803/article/details/114730777 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql 二进制日志删除_通过 MySQL 二进制日志恢复删除的记录
下一篇:mac mama mysql安装_妈妈再也不用担心,我被问MySQL隔离级别了!!!

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月13日 03时52分15秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章