jsp网页快照,根据url获取页面缩略图
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.net.*,java.util.*,java.io.*"%>
<%
try{
String sCurrentLine;
String sTotalString;
sCurrentLine="";
sTotalString="";
String url = request.getParameter("url"); //取得要快照的网页的网址,即问号后面的url参数
InputStream l_urlStream;
URL l_url = new URL(url);
HttpURLConnection l_connection = (HttpURLConnection)l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString += sCurrentLine;
}
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".htm";
fileame = request.getRealPath("/")+"snap/"+fileame; //生成的html文件保存路径(snap文件夹下面)
FileOutputStream fileoutputstream = new FileOutputStream(fileame); //建立文件输出流
byte tag_bytes[] = sTotalString.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}
%>