前段时间项目用到APP端访问后台需读取Oracle-clob类型字段,由于直接查询出clob类型读取不了,后来找了很多资料,终于完成了!现在记录一下!
不多说了 直接上代码:
//转换clob类型
public String ClobToString(Clob clob) throws SQLException, IOException {
String reString = "";
Reader is = clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
if(br!=null){
br.close();
}
if(is!=null){
is.close();
}
return reString;
}
实现:
List<Map<String, String>> news_list = GetNewslist(map);
for(int i=0;i<news_list.size();i++)
{
Map<String, String> maps = news_list.get(i);
Object content = maps.get("news_content");
if(content!=null&&!content.equals("")){
String contents = ClobToString((Clob)content);//转换clob类型
contents=contents.replaceAll("</?[^/?(br)|(p)][^><]*>","");//保留br标签和p标签
String content1 = trimStyle(contents);//去除style标签
String content2 = trimClass(content1);//去除class标签
maps.put("news_content", content2);
}
}
另有去除标签方法,一并放上:
//去除style标签
public static String trimStyle(String content) {
String regEx = " style=\"(.*?)\"";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(content);
String okContent = "";
if (m.find()) {
okContent = m.replaceAll("");
return okContent;
}
return content;
}
//去除class标签
public static String trimClass(String content) {
String regEx = " class=\"(.*?)\"";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(content);
String okContent = "";
if (m.find()) {
okContent = m.replaceAll("");
return okContent;
}
return content;
}