java URL链接,下载所访问的网页

 

 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
 * URL链接,下载所访问的网页
 */
public class UrlConnection {

    public static void main(String[] args) {
    	
    	//URL地址数组
    	String[] urls = new String[]{"http://991690137.iteye.com/blog/1946378",
    	"http://991690137.iteye.com/blog/1946173","http://991690137.iteye.com/blog/1946149",
    	"http://991690137.iteye.com/blog/1946149","http://991690137.iteye.com/blog/1946119",
    	"http://991690137.iteye.com/blog/1946123","http://991690137.iteye.com/blog/1946133",
    	"http://991690137.iteye.com/blog/1944394","http://991690137.iteye.com/blog/1944245",
    	"http://991690137.iteye.com/blog/1942772","http://991690137.iteye.com/blog/1942767",
    	"http://991690137.iteye.com/blog/1942736","http://991690137.iteye.com/blog/1942736",
    	"http://991690137.iteye.com/blog/1942717"};
    	
    	//遍历数组
    	for(int i=0;i<urls.length;i++){
    		//循环访问100次
    		for(int j=1;j<100;j++){
        		System.out.println("beging...");
        		//将访问的网页下载并保存在L:/html/indexn.html
        		DownLoadPages(urls[i],"L:/html/index"+i+".html");
        		System.out.println("end.");
        		try {
        			//每次访问休息1.5s,目的是减轻所访问网页所在服务器的压力,再者访问过快会导致很多意想不到的问题
    				Thread.sleep(1500);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
        	}
    	}
    }

    /**
     * 下载网页 或 文件
     * @param urlStr 网页地址 比如: http://www.163.com
     * @param outPath 文件输出路径
     */
    public static void DownLoadPages(String urlStr, String outPath)
    {
        /** 读入的输入流是字节流
         * chByte是读入的每个字节所转成int类型的表示
         * */
        int chByte = 0;
        
        /** 网络的url地址 */
        URL url = null;
        
        /** http连接 */
        HttpURLConnection httpConn = null;
        
        /** 输入流 */
        InputStream in = null;
        
        /** 文件输出流 */
        FileOutputStream out = null;
        try
        {
            url = new URL(urlStr);
            httpConn = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(true);
            httpConn.setRequestMethod("GET"); 
            httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); 
            
            in = httpConn.getInputStream();
            out = new FileOutputStream(new File(outPath));

            chByte = in.read();
            while (chByte != -1)
            {
                out.write(chByte);
                chByte = in.read();
            }
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                out.close();
                in.close();
                httpConn.disconnect();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值