
package com.fr.function;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import com.fr.data.core.db.*;
import com.fr.script.AbstractFunction;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Primitive;
import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class getK3ZipRTF extends AbstractFunction{
private static final long serialVersionUID = 1L;
@Override
public Object run(Object[] args) {
// TODO Auto-generated method stub
if(ArrayUtils.getLength(args)!=1) {
return Primitive.ERROR_VALUE;
}
return getK3Rtf2Html((BinaryObject)args[0]);
}
/***
*
* @param data 从数据库取得的IMAGE类型数据
* @return 解压后的字节数组
*/
private static byte[] decompress(BinaryObject data) {
byte[] bt=data.getBytes();
Inflater decompresser=new Inflater(); //压缩对象
decompresser.reset(); //清空对象数据
decompresser.setInput(bt);//入入待解压二进制数
int len=-1;
byte[] buf=new byte[1024];
try {
len=decompresser.inflate(buf); //解压后的数据写入字节数组,并获得数据长度
} catch (DataFormatException e) {
e.printStackTrace();
}
decompresser.end(); //结束解压
return Arrays.copyOf(buf,len);
}
private static String getK3Rtf2Html(BinaryObject ins)
{
String insNew=null;
byte[] b=decompress(ins); //解压后的字节数组
InputStream is=new ByteArrayInputStream(b); //读入InputStream
try {
insNew= rtfToHtml(is); //将输入流转为HTML
} catch (IOException e) {
e.printStackTrace();
}
try {
insNew= new String(insNew.getBytes("Cp1252"), "GB2312"); //编码转换为GB2312
insNew=convertText(insNew); //提取元素标签并进行转换
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return insNew;
}
/***
*
* @param in输入流
* @return 输出String格式的HTML
* @throws IOException
*/
private static String rtfToHtml(InputStream in) throws IOException {
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
try {
kitRtf.read(in, p.getDocument(), 0); //inputstream转成文档
kitRtf = null;
EditorKit kitHtml = p.getEditorKitForContentType("text/html");
Writer writer = new StringWriter();
kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
return writer.toString();
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}
/***
*
* @param HTML格式的字串
* @return 处理过的字串
*/
private static String convertText(String text) {
if(text!=null) {
Document doc=Jsoup.parse(text);
Elements links=doc.getElementsByTag("span");
StringBuffer buffer=new StringBuffer();
for(Element link:links) {
buffer.append(link.text().trim()+"<br />");
}
return buffer.toString().trim();
}
return null;
}
}
原文:https://www.cnblogs.com/liangyuwen/p/13130125.html