利用java实现读取html文件中的body标签和内容

2023-05-16,,

利用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;

@Component
public 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("<img\\s+"); // <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=");
    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标签和内容问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注本站行业资讯频道了解更多相关知识。

《利用java实现读取html文件中的body标签和内容.doc》

下载本文的Word格式文档,以方便收藏与打印。