去除富文本格式


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("需要處理的字符串);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM