在後臺去除HTML的樣式;例如富文字編輯器的
阿新 • • 發佈:2018-12-05
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)); } }
上面的是從別人那裡轉的地址是:https://www.cnblogs.com/cnsevennight/p/4468055.html
前言:本人在實現業務邏輯的時候,需要在後臺把帶HTML標籤(富文字)資料的文章擷取成文章的摘要,涉及到怎麼處理帶HTML標籤資料 ,在網上一共找到了兩種解決方法:
1、呼叫HtmlParser外掛
HtmlParser 簡介
htmlparser是一個純的java寫的html解析的庫,主要用於改造或提取html。用來分析抓取到的網頁資訊是個不錯的選擇,遺憾的是參考文件太少。
專案主頁:http://htmlparser.sourceforge.net/ API文件: http://htmlparser.sourceforge.net/javadoc/index.html
參考部落格:http://blog.csdn.net/fancy3013/article/details/50965112
2、引用一個方法直接去掉HTML標籤
這也是我在專案所用的,是別人寫好的一個方法。
public String parseHtml(String html,int length) { if(html == null || html == "") { return html = "空"; }else { if(html.length()<length){ return html; }else { /* * <.*?>為正則表示式,其中的.表示任意字元,*?表示出現0次或0次以上,此方法可以去掉 雙頭標籤(雙頭針對於殘缺的標籤) * "<.*?"表示<尖括號後的所有字元,此方法可以去掉殘缺的標籤,及後面的內容 * " ",若有多種此種字元,可用同一方法去除 */ html = html.replaceAll("<.*?>", " ").replaceAll("", ""); html = html.replaceAll("<.*?", ""); return (html.substring(0, length) + "..."); } }
參考自:https://blog.csdn.net/lq13457309725/article/details/79578326?utm_source=copy