告别文章摘要的烦恼之博客系统

package com.chendaojun.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ParseHtml { public static void main(String[] args){ //可以将注释打开逐个试验 ParseHtml ph = new ParseHtml(); String html=""; //打开下面两行可进行连接mysql并解析html //html=ph.getHtmlFromMysql(); //System.out.println(ph.parseHtml(html)); //System.out.println(ph.parseHtml(html,300)); //打开下面两行可进行获得路径文件内容并解析html,路径根据实际修改 //html=ph.getHtml("E:\\1478300.html"); //System.out.println(ph.parseHtml(html)); //System.out.println(ph.parseHtml(html,300)); //指定长度直接解析 //html=ph.parseHtml("<p>sdfsdf</p><br><div>sdfsdfsdf</div>",10); //System.out.println(html); //直接解析 html=ph.parseHtml("<p>sdfsdf</p><br><div>sdfsdfsdf</div>sdflksdflksdjfk<dkf"); System.out.println(html); } //从mysql中取出在线编辑器存进去的html文章 public String getHtmlFromMysql(){ String url="jdbc:mysql://localhost:3306/blog"; String userName="root"; String passWord="root"; String className="com.mysql.jdbc.Driver"; String sql="select text from blog where id=5"; String html=""; Connection conn=null; Statement stmt=null; ResultSet rs=null; try{ Class.forName(className); conn=DriverManager.getConnection(url,userName,passWord); stmt=conn.createStatement(); rs=stmt.executeQuery(sql); while(rs.next()){ //获得html内容 html=rs.getString("text"); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); rs=null; } if(stmt!=null){ stmt.close(); stmt=null; } if(conn!=null){ conn.close(); conn=null; } }catch(Exception e){ e.printStackTrace(); } } return html; } //从指定路径读取html文件 public String getHtml(String filePath) { String html = ""; FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { File file = new File(filePath); fis = new FileInputStream(file); isr = new InputStreamReader(fis); br = new BufferedReader(isr); String bRead = ""; while ((bRead = br.readLine()) != null) { html += bRead; } } catch (Exception e) { e.printStackTrace(); } finally { try { if(br!=null){ br.close(); br=null; } if(isr!=null){ isr.close(); isr=null; } if(fis!=null){ fis.close(); fis=null; } } catch (Exception e) { e.printStackTrace(); } } return html; } //任意html,残缺不全也可以 public String parseHtml(String html) { /* * <.*?>为正则表达式,其中的.表示任意字符,*?表示出现0次或0次以上,此方法可以去掉双头标签(双头针对于残缺的标签) * "<.*?"表示<尖括号后的所有字符,此方法可以去掉残缺的标签,及后面的内容 * "",若有多种此种字符,可用同一方法去除 */ html = html.replaceAll("<.*?>", " ").replaceAll("", " "); html = html.replaceAll("<.*?", ""); return (html + "..."); } //可以指定截取长度 public String parseHtml(String html,int length) { if(html.length()<length){ return "截取长度超过文件内容总长"; } return parseHtml(html.substring(0, length)); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值