用jsp,读远程文件,保存到本地。
如果你的网站动态内容比较多(频频读取数据库,而数据更新较少,一天一次),你可以将首页转化为HTML静态文件,加快网页下载速度,下面这个JSP文件将index.jsp转化为index.html。
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Properties"%>
<%
//远程文件路径 String s1 = "http://www.java3z.com/cwbwebhome/index.jsp"; //本地存放路径 String s2 = getServletContext().getRealPath("/")+"index.html"; URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(s2); //make proxy //String proxy = "192.168.224.12"; //String port = "8080"; //Properties systemProperties = System.getProperties(); //systemProperties.setProperty("http.proxyHost",proxy); //systemProperties.setProperty("http.proxyPort",port); try{ urlfile = new URL(s1); httpUrl = (HttpURLConnection)urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); }catch(Exception e){ System.out.println(e.toString()); } try{ bos = new BufferedOutputStream(new FileOutputStream(f));; byte[] b = new byte[1024]; while(bis.read(b)!=-1) { bos.write(b); } }catch(Exception e){ System.out.println(e.toString()); }finally{ try{ bos.flush(); bis.close(); httpUrl.disconnect(); }catch(Exception e){ out.println(e.toString()); } } %> OK!!
其中
//make proxy
String proxy = "192.168.224.12";//防火墙地址
String port = "8080"; //防火墙端口
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
这一段是如果你的机器设定了防火墙,需要加上,如果是直接连上网,就不用。
↑返回目录
前一篇: 用bean保存表单状态
后一篇: FCKeditor在线编辑器的使用