日期:2014-05-16 浏览次数:20337 次
public class OnepageUtil { private int size = 800;// 每页显示内容量 private int currentPage = 1;//设置第一页为初始页 public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } private int pages = 0; private int length = 0; String PageInfo = ""; //前台显示的文章内容 public String getSubContent(String content){ ArrayList newContent = new ArrayList(); newContent = acticlePager(content); String cont = "文章已结束..."; pages = countPages(content); if (currentPage > pages) currentPage = pages; try{ cont = newContent.get(currentPage-1).toString(); }catch(Exception e){ } return cont; } //将文章内容分成数组 public ArrayList acticlePager(String content) { ArrayList list = new ArrayList(); boolean end = true; length = content.length(); while (end) { String temp = ""; /** * 如果内容已经少于默认数,就直接作为结尾返回 */ if (size >= length) { temp = content; list.add(temp); break; } /** * temp为本次截取内容段 temp2为余下的内容段 */ temp = content.substring(0, size); String temp2 = content.substring(size + 1, length); String session = temp; int a = 0; int b = 0; /** * 首先计算 <和>是否相等 */ while (temp.indexOf(" <") > -1) { a++; temp = temp.substring(temp.indexOf(" <") + 1, temp.length()); } temp = session; while (temp.indexOf(">") > -1) { b++; temp = temp.substring(temp.indexOf(">") + 1, temp.length()); } if (a != b) { int p = temp2.indexOf(">"); temp = content.substring(0, size + p + 2); temp2 = content.substring(size + p + 2, length); session = temp; } /** * 如果相等就再计算 <P和 * </p> * 是否吻合 */ if (a == b) { a = 0; b = 0; temp = session; while (temp.indexOf(" <P") > -1) { a++; temp = temp.substring(temp.indexOf(" <") + 1, temp.length()); } temp = session; while (temp.indexOf(" </P") > -1) { b++; temp = temp.substring(temp.indexOf(">") + 1, temp.length()); } if (a == b) { break; } if (a != b) { int p = temp2.indexOf(" </P>"); temp = content.substring(0, size + p + 5); try { if ((size + p + 5) < length) temp2 = content.substring(size + p + 5, length); } catch (Exception e) { temp2 = ""; } } } /** * 余下内容更新 */ content = temp2; length = content.length(); // System.out.println("cut after:"+content); System.out.println("cut after:" + temp); list.add(temp); /** * 如果不存在余下内容了就结束本次操作 */ if (temp2.equals("") || temp2.length() < 1) end = false; } return list; } //计算总页数 public int countPages(String content){ length = co