java讀取html文件,并獲取body中所有的標簽及內容的案例
這里的獲取的是html文件中body中的所有標簽以及內容
package com.lmt.service.file;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.Reader;import org.springframework.stereotype.Component;import com.lmt.config.UrlConstants;@Componentpublic class ParseFile { /** * 解析html文件 * @param file * @return */ public String readHtml(File file){ String body = ''; try { FileInputStream iStream = new FileInputStream(file); Reader reader = new InputStreamReader(iStream); BufferedReader htmlReader = new BufferedReader(reader); String line; boolean found = false; while (!found && (line = htmlReader.readLine()) != null) {if (line.toLowerCase().indexOf('<body') != -1) { // 在<body>的前面可能存在空格 found = true;} } found = false; while (!found && (line = htmlReader.readLine()) != null) {if (line.toLowerCase().indexOf('</body') != -1) { found = true;} else { // 如果存在圖片,則將相對路徑轉換為絕對路徑 String lowerCaseLine = line.toLowerCase(); if (lowerCaseLine.contains('src')) {//這里是定義圖片的訪問路徑 String directory = 'D:/test'; // 如果路徑名不以反斜杠結尾,則手動添加反斜杠 /*if (!directory.endsWith('')) { directory = directory + ''; }*/ // line = line.substring(0, lowerCaseLine.indexOf('src') + 5) + directory + line.substring(lowerCaseLine.indexOf('src') + 5); /*String filename = extractFilename(line); line = line.substring(0, lowerCaseLine.indexOf('src') + 5) + directory + filename + line.substring(line.indexOf(filename) + filename.length()); */ // 如果該行存在多個<img>元素,則分行進行替代 String[] splitLines = line.split('<imgs+'); // <img后帶一個或多個空格 // 因為java中引用的問題不能使用for each for (int i = 0; i < splitLines.length; i++) { if (splitLines[i].toLowerCase().startsWith('src')) {splitLines[i] = splitLines[i].substring(0, splitLines[i].toLowerCase().indexOf('src') + 5) + directory + splitLines[i].substring(splitLines[i].toLowerCase().indexOf('src') + 5); } }// 最后進行拼接 line = ''; for (int i = 0; i < splitLines.length - 1; i++) { // 循環次數要-1,因為最后一個字符串后不需要添加<img line = line + splitLines[i] + '<img '; } line = line + splitLines[splitLines.length - 1]; } body = body + line + 'n';} } htmlReader.close(); // System.out.println(body); } catch (Exception e) { e.printStackTrace(); } return body; } /** * * @param htmlLine 一行html片段,包含<img>元素 * @return 文件名 */ public static String extractFilename(String htmlLine) { int srcIndex = htmlLine.toLowerCase().indexOf('src='http://www.aoyou183.cn/bcjs/); if (srcIndex == -1) { // 圖片不存在,返回空字符串 return ''; } else { String htmlSrc = htmlLine.substring(srcIndex + 4); char splitChar = ’'’; // 默認為雙引號,但也有可能為單引號 if (htmlSrc.charAt(0) == ’’’) {splitChar = ’’’; } String[] firstSplit = htmlSrc.split(String.valueOf(splitChar)); String path = firstSplit[1]; // 第0位為空字符串 String[] secondSplit = path.split('[/]'); // 匹配正斜杠或反斜杠 return secondSplit[secondSplit.length - 1]; } } }
補充知識:StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
jar包沒有正確導入
1、在 build path 中添加
2、如果這里不添加在編譯的時你的jar包將不會被導入
3、如果依然沒有成功請刪除user jar包重新導入
以上這篇java讀取html文件,并獲取body中所有的標簽及內容的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
