久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Java 根據網絡URL獲取該網頁上面所有的img標簽并下載圖片

瀏覽:105日期:2022-08-21 14:55:50

說明:根據網絡URL獲取該網頁上面所有的img標簽并下載符合要求的所有圖片

所需jar包:jsoup.jar

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import java.util.UUID;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;/** * 圖片批量下載工具類 * @author Marydon * @create time 2016-9-3下午2:01:03 * @update time 2017年9月30日11:07:02 * @E-mail:[email protected] */public class ImgDownloadUtil { /** * 根據URL獲取網頁DOM對象 * @param url * 網址 * @return DOM對象 */ public static Document getHtmlDocument(String url) { Document document = null; URL urlObj = null; try { // 1.建立網絡連接 urlObj = new URL(url); // 2.根據url獲取Document對象 document = Jsoup.parse(urlObj, 5000);// 單位:毫秒超時時間 } catch (MalformedURLException e) { System.out.println('世界上最遙遠的距離就是沒有網,檢查設置!'); e.printStackTrace(); } catch (IOException e) { System.out.println('您的網絡連接打開失敗,請稍后重試!'); e.printStackTrace(); } return document; } /** * 根據URL獲取網頁源碼 * @param url * 網址 * @return 網頁源碼 */ public static String getHtmlText(String url) { String htmlText = ''; Document document = null; URL urlObj = null; try { // 1.建立網絡連接 urlObj = new URL(url); // 2.根據url獲取Document對象 document = Jsoup.parse(urlObj, 5000);// 單位:毫秒超時時間 // 3.根據dom對象獲取網頁源碼 htmlText = document.html(); } catch (MalformedURLException e) { System.out.println('世界上最遙遠的距離就是沒有網,檢查設置!'); e.printStackTrace(); } catch (IOException e) { System.out.println('您的網絡連接打開失敗,請稍后重試!'); e.printStackTrace(); } return htmlText; } /** * 操作Dom對象獲取圖片地址 * @param document * Dom對象 * @return 圖片地址集合 */ public static List<String> getImgAddressByDom(Document document) { // 用于存儲圖片地址 List<String> imgAddress = new ArrayList<String>(); if (null != document) { // <img src='http://www.gepszalag.com/bcjs/5670.html' alt='' width='' height=''/> // 獲取頁面上所有的圖片元素 Elements elements = document.getElementsByTag('img'); String imgSrc = ''; // 迭代獲取圖片地址 for (Element el : elements) {imgSrc = el.attr('src');// imgSrc的內容不為空,并且以http://開頭if ((!imgSrc.isEmpty()) && imgSrc.startsWith('http://')) { // 將有效圖片地址添加到List中 imgAddress.add(imgSrc);} } } return imgAddress; } /** * 根據網絡URL下載文件 * @param url * 文件所在地址 * @param fileName * 指定下載后該文件的名字 * @param savePath * 文件保存根路徑 */ public static void downloadFileByUrl(String url, String fileName, String savePath) { URL urlObj = null; URLConnection conn = null; InputStream inputStream = null; BufferedInputStream bis = null; OutputStream outputStream = null; BufferedOutputStream bos = null; try { // 1.建立網絡連接 urlObj = new URL(url); // 2.打開網絡連接 conn = urlObj.openConnection(); // 設置超時間為3秒 conn.setConnectTimeout(3 * 1000); // 防止屏蔽程序抓取而返回403錯誤 conn.setRequestProperty('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)'); // 3.得到輸入流 inputStream = conn.getInputStream(); bis = new BufferedInputStream(inputStream); // 文件保存位置 File saveDir = new File(savePath); if (!saveDir.exists()) {saveDir.mkdirs(); } // 文件的絕對路徑 String filePath = savePath + File.separator + fileName; File file = new File(filePath); // 4. outputStream = new FileOutputStream(file); bos = new BufferedOutputStream(outputStream); byte[] b = new byte[1024]; int len = 0; while ((len = bis.read(b)) != -1) {bos.write(b, 0, len); } System.out.println('info:' + url + ' download success,fileRename=' + fileName); } catch (MalformedURLException e) { System.out.println('世界上最遙遠的距離就是沒有網,檢查設置'); System.out.println('info:' + url + ' download failure'); e.printStackTrace(); } catch (IOException e) { System.out.println('您的網絡連接打開失敗,請稍后重試!'); System.out.println('info:' + url + ' download failure'); e.printStackTrace(); } finally {// 關閉流 try {if (bis != null) {// 關閉字節緩沖輸入流 bis.close();}if (inputStream != null) {// 關閉字節輸入流 inputStream.close();}if (bos != null) {// 關閉字節緩沖輸出流 bos.close();}if (outputStream != null) {// 關閉字節輸出流 outputStream.close();} } catch (IOException e) {e.printStackTrace(); } } }}

測試

public static void main(String[] args) { // 1.確定網址 String url = 'http://www.cnblogs.com/Marydon20170307/p/7402871.html'; // 2.獲取該網頁的Dom對象 Document document = getHtmlDocument(url); // 3.獲取該網頁所有符合要求的圖片地址 List<String> imgAddresses = getImgAddressByDom(document); String imgName = ''; String imgType = ''; // 4.設置圖片保存路徑 String savePath = 'C:/Users/Marydon/Desktop'; // 5.批量下載圖片 for (String imgSrc : imgAddresses) { // 5.1圖片命名:圖片名用32位字符組成的唯一標識 imgName = UUID.randomUUID().toString().replace('-', ''); // 5.2圖片格式(類型) imgType = imgSrc.substring(imgSrc.lastIndexOf('.')); imgName += imgType; // 5.3下載該圖片 downloadFileByUrl(imgSrc, imgName, savePath); }}

以上就是Java 根據網絡URL獲取該網頁上面所有的img標簽并下載圖片的詳細內容,更多關于java 下載網絡圖片的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 久久精品国产99国产 | 色玖玖综合 | 色吧av| 中文字幕一区二区三区四区五区 | 久久久久久国产 | 欧美一区二区在线免费观看 | 欧美一级片在线观看 | 日本精品在线观看 | 精品九九九| 欧美日韩成人在线 | 99热新| 国产色黄视频 | 羞羞视频在线免费 | 伊人网站| 97成人在线 | 久久综合一区 | 91色站 | www.久久久| 99国产精品视频免费观看一公开 | 一级片av | 91精品国产一区二区三区四区在线 | 农村妇女毛片精品久久久 | 欧美一区二区三区免费视频 | 国产馆一区二区 | 欧美日韩不卡合集视频 | vagaa欧洲色爽免影院 | 国产99久久精品一区二区永久免费 | 在线观看成人国产 | 色av一区 | 色欧美综合 | 亚洲高清网| 国产www精品| 亚洲免费视频在线观看 | 久久视频在线看 | 91精品一区 | 国产乡下妇女做爰视频 | 另类亚洲专区 | 久久三区| 一区二区三区在线观看视频 | 在线观看国产视频 | 亚洲精品在线视频 |