当前位置: 移动技术网 > IT编程>开发语言>Java > 去除富文本格式

去除富文本格式

2019年03月21日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网