⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 secondpage.java

📁 页面抓取新闻并自动生成网页的代码 有兴趣的可以看看,很有意思的东西
💻 JAVA
字号:
package admin;
import java.io.*;
import java.sql.*;


public class SecondPage{
	private String artType; //对应名称
	private String pAddress; //存放路径
  private StringBuffer cont=new StringBuffer();//替换的文章超链接内容	
  private ResultSet rs=null;
  private int rsSize=0;
  private int maxperpages=30;  //每页最大显示数
  private String path;   //系统的物理地址 
  private String filePath; //摸板路径
  private String filecontent; //所属类别,中文
  	
	public void setItem(String artType,ResultSet rs,String path,String filecontent){//初始化
		this.artType=artType;
		this.pAddress=artType;
		this.rs=rs;
		this.path=path;
		this.filecontent=filecontent;
		filePath=path+"templet\\secondPage.html";//摸板路径
		}
		
	public StringBuffer setContB(int currentPage,int n){
		StringBuffer contButton=new StringBuffer();//替换的底部导航选择条内容
		contButton.append("<table align='center'><tr><td>");
		if(currentPage<2)contButton.append("首页 上一页&nbsp;");
		else{
			contButton.append("<a href='index-1.html'>首页</a>&nbsp;");
			contButton.append("<a href='index-"+(currentPage-1)+".html'>上一页</a>&nbsp;");
		  }
			if((n-currentPage)<1)contButton.append("&nbsp;下一页&nbsp;尾页&nbsp;");			
			else contButton.append("<a href='index-"+(currentPage+1)+".html'>下一页</a>&nbsp;<a href='index-"+n+".html'>尾页</a>&nbsp;");			
			contButton.append("页次:<strong><font color=red>"+currentPage+"</font>/"+n+"</strong>页 &nbsp;");
			contButton.append("<b>"+maxperpages+"</b>篇文章/页&nbsp;");
			contButton.append("转到:<select name='page' size='1' onchange=\"javascript:window.location='index-'+this.options[this.selectedIndex].value+'.html';\">");
			for(int i=1;i<=n;i++){
				contButton.append("<option value='"+i+"' ");
				if(i==currentPage)contButton.append("selected");
				contButton.append(">");
				contButton.append("第"+i+"页</option>");
				}
			contButton.append("</select></td></tr></table>");
			
		return contButton;	 
	  }
		
	public void savePage()throws Exception{
		int n=1;
	 	rs.last();
	 	rsSize=rs.getRow();
	 	rs.beforeFirst(); //数据库查询记录游标返回第一行前面
	 	if(rsSize%maxperpages==0)n=rsSize/maxperpages;
	 	else n=rsSize/maxperpages+1; 
    int newn=n;
   	int currentPage=1;  //当前页数
    int count=1; //每页显示最大条数     
   	while(rs.next()){
  // 		int newn=n;
   		if(count<=30){
   			String buttonCont; //底部内容
   			String templateContent="";
   			String title2=rs.getString("title"); //题目
   			String fileName=rs.getString("fileName"); //文件名
   			String inTime=rs.getString("inTime");  //时间
   			String title=new String(); // 显示切割后的文件名
   			if(title2.length()>38){
   				title=title2.substring(0,38)+"…";
   				}else title=title2;
   			cont.append("<img src='../../images/article_common.gif' alt='普通文章'>&nbsp;");
   			cont.append("<a href='/yingke/content/"+artType+"/"+fileName+"' ");  			
   			cont.append("title='文章标题:"+title2+"' target='_blank'>");
   			cont.append(title+"</a> ");
   			cont.append("("+inTime+")");
   			if(count!=30) cont.append("<br>");
   			if(count==30||rs.isLast()){
   				buttonCont=new String(setContB(1,newn));  //得到底部条块  				
   				FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
   				int lenght = fileinputstream.available();
          byte bytes[] = new byte[lenght];
          fileinputstream.read(bytes);
          fileinputstream.close();
          String newCont=new String(cont);
          templateContent = new String(bytes);
          templateContent=templateContent.replaceAll("###leibie###",filecontent);
          templateContent=templateContent.replaceAll("###neirong###",newCont);
          templateContent=templateContent.replaceAll("###bottomTable###",buttonCont);
   				String htmlName="index-1.html";
   				if(currentPage!=1)htmlName="index-"+(currentPage)+".html"; 
   				String fileSaveName = path+"content\\"+pAddress+"\\"+htmlName;//生成的html文件保存路径
   				FileOutputStream fileoutputstream = new FileOutputStream(fileSaveName);//建立文件输出流
   				byte tag_bytes[] = templateContent.getBytes();
   				fileoutputstream.write(tag_bytes);
   				fileoutputstream.close();
   				currentPage++;
   				count=1;
   				cont=new StringBuffer();
   				}
   			count++;
   			}
   		}
   	if(rsSize==0){
   		String templateContent="";
   		String newCont="";
   		String buttonCont;
   		buttonCont=new String(setContB(currentPage,newn));  //得到底部条块
   		FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
   		int lenght = fileinputstream.available();
   		byte bytes[] = new byte[lenght];
   		fileinputstream.read(bytes);
      fileinputstream.close();
      templateContent = new String(bytes);
      templateContent=templateContent.replaceAll("###leibie###",filecontent);
      templateContent=templateContent.replaceAll("###neirong###",newCont);
      templateContent=templateContent.replaceAll("###bottomTable###",buttonCont);
   		String htmlName="index-1.html";
   		String fileSaveName = path+"content\\"+pAddress+"\\"+htmlName;//生成的html文件保存路径
   		FileOutputStream fileoutputstream = new FileOutputStream(fileSaveName);//建立文件输出流
   	  byte tag_bytes[] = templateContent.getBytes();
   		fileoutputstream.write(tag_bytes);
   		fileoutputstream.close();
   		}			
		}	
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -