zoukankan      html  css  js  c++  java
  • 去除富文本格式

    1、写一个公共类

    package com.boyutec.oss.sys.util;

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;

    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.parser.ParserDelegator;

    public class Html2Text extends HTMLEditorKit.ParserCallback {

      private static Html2Text html2Text = new Html2Text();

      StringBuffer s;

      public Html2Text() {
      }

      public void parse(String str) throws IOException {

        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        // the third parameter is TRUE to ignore charset directive
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
      }

      public void handleText(char[] text, int pos) {
        s.append(text);
      }

      public String getText() {
        return s.toString();
      }

      public static String getContent(String str) {
        try {
          html2Text.parse(str);
        } catch (IOException e) {
          e.printStackTrace();
        }
        return html2Text.getText();
      }

    }

    2、可以直接调用:Html2Text.getContent("需要处理的字符串);

  • 相关阅读:
    10月15日模拟赛题解
    NOIp初赛题目整理
    【meet in the mid】【qbxt2019csp刷题班day1C】birthday
    【字符串】 manacher算法
    【border树】【P2375】动物园
    【border相关】【P3426】 [POI2005]SZA-Template
    【字符串】 Z-algorithm
    【字符串】KMP
    【神奇性质】【P5523】D [yLOI2019] 珍珠
    【线段树】【P5522】[yLOI2019] 棠梨煎雪
  • 原文地址:https://www.cnblogs.com/h-wei/p/10565665.html
Copyright © 2011-2022 走看看