码迷,mamicode.com
首页 > 编程语言 > 详细

java根据html生成摘要

时间:2015-04-30 10:26:01      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 

转自:http://java.freesion.com/article/48772295755/

开发一个系统,需要用到这个,根据html生成你指定多少位的摘要

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));
    }
}

 

java根据html生成摘要

标签:

原文地址:http://www.cnblogs.com/cnsevennight/p/4468055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!