普通网页抓取

package com.hidata.web.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

/**
* jsp 转换成 html
* 把动态网页转换成静态网页
*/
public class JspToHtml {

        /**
         * 根据url生成静态页面
         *
         * @param urlStr  动态文件路经 如:[url]http://www.163.com/x.jsp[/url]
         * @param path 文件存放路经如:x:\\abc\bbb.html
         * @return
         */
        public static boolean JspToHtmlByURL(String urlStr, String path) {
                //从utl中读取html存为str
                String str = "";
                try {
                        URL url = new URL(urlStr);
                        URLConnection uc = url.openConnection();
                        uc.setConnectTimeout(20000);
                        uc.setReadTimeout(20000);
                        InputStream is = uc.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
//                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String tmp = null;
                        while ((tmp = br.readLine()) != null) {
                                str += tmp + "\n";
                        }
                        is.close();
                        //写入文件
                        File f = new File(path);
                        File parent  = f.getParentFile();
                        if (!parent.exists()) {
                        	parent.mkdirs();
                        } else {
                        	if(f.exists()) {
                        		f.delete();
                        	}
                        }
//                        BufferedWriter o = new BufferedWriter(new FileWriter(f));
                        
                        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
                        BufferedWriter o=new BufferedWriter(write);  
                        
                        System.out.println("======================");
                        System.out.println(str);
                        System.out.println("======================");
                        str="<%@ page contentType=\"text/html;charset=UTF-8\"%>" + str;
                        o.write(str);
                        o.close();
                        str = "";
                        return true;
                } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                }
        }
        
        public static boolean copyFile(String sourceFilePath, String destFilePath) {
        	  //从utl中读取html存为str
            String str = "";
            try {
                    InputStream is = new FileInputStream(new File(sourceFilePath));
                    BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                    String tmp = null;
                    while ((tmp = br.readLine()) != null) {
                            str += tmp + "\n";
                    }
                    is.close();
                    //写入文件
                    File f = new File(destFilePath);
                    File parent  = f.getParentFile();
                    if (!parent.exists()) {
                    	parent.mkdirs();
                    } else {
                    	if(f.exists()) {
                    		f.delete();
                    	}
                    }
                    OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
                    BufferedWriter o=new BufferedWriter(write);  
                    
                    o.write(str);
                    o.close();
                    return true;
            } catch (Exception e) {
                    e.printStackTrace();
                    return false;
            }
        }
        
        /**
         * 检测文件是否存在
         * @param filePath
         * @return
         */
        public static boolean fileExists(String filePath) {
        	File file = new File(filePath);
        	if(file.exists()) {
        		return true;
        	}
        	return false;
        }

        /**
         * 测试main 函数
         *
         * @param arg
         */
        public static void main(String[] arg) {
                long begin = System.currentTimeMillis();
                //循环生成20个html文件
//                String url = "http://www.hao123.com/";//模板文件地址
//                String url = "http://localhost:8080/create/test";//模板文件地址
//                String url = "http://www.vao345.com/";//模板文件地址
                String url = "http://www.baidu.com/";//模板文件地址
                String savepath = "e:/temp/test/baidu.html";//生成文件地址
                JspToHtmlByURL(url, savepath);
                System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
        }
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值