下载网页

来源:互联网 发布:朴素贝叶斯算法吴恩达 编辑:程序博客网 时间:2024/05/21 12:26
package test1109;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.URI;import org.apache.commons.httpclient.methods.GetMethod;/** * 网站获取 * * @author Lee * */public class TestHttpClient {public static void main(String[] args) {String str = "http://www.baidu.com";//服务器地址String outPath = "D:/";//输出路径String suffix = ".html";//文件后缀int maxLength = 1;//文件数量, 按照数值递增编号int beginIndex = 1;//开始下标String urlStr = "";FileOutputStream fos = null;BufferedOutputStream bos = null;SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");long beginTime = System.currentTimeMillis();// 开始时间System.out.println("==== 读取开始时间:" + dateFormat.format(new Date(beginTime)) + " ==============");try {URI url = null;HttpClient client = new HttpClient();GetMethod get = null;for (int i = beginIndex; i <= maxLength; i++) {urlStr = str +i;//urlStr = str ;url = new URI(urlStr, false, "UTF-8");get = new GetMethod(url.toString());client.executeMethod(get);if (get.getStatusCode() == 200) {byte[] data = get.getResponseBody();fos = new FileOutputStream(outPath+i+suffix);bos = new BufferedOutputStream(fos);bos.write(data);}}} catch (Exception e) {e.printStackTrace();}finally {try {if(bos!=null){bos.close();}} catch (IOException e) {e.printStackTrace();}try {if(fos!=null){fos.close();}} catch (IOException e) {e.printStackTrace();}}long endTime = System.currentTimeMillis();// 结束时间double second = (double) ((endTime - beginTime) / 1000); // 用时:秒System.out.println("==== 读取结束时间:" + dateFormat.format(new Date(endTime)) + " ==============");System.out.println("==== 读取用时:" + second + " 秒 ==============");}}
0 0