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("需要处理的字符串);
如对本文有疑问, 点击进行留言回复!!
springcloud和Eureka整合时报错:Unable to start embedded Tomcat
网友评论