一个简单的下载html页面的程序

来源:互联网 发布:服务器防火墙软件 编辑:程序博客网 时间:2024/05/17 06:34
package com.xdd.RetrivePage;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


public class RetrivePage {


private static HttpClient httpClient = new HttpClient();
//设置代理服务器
static{
//设置代理服务器的ip地址和端口号
//httpClient.getHostConfiguration().setProxy("127.0.0.1", 8080);
}

private static boolean downloadPage(String path)throws HttpException,
IOException{
InputStream input = null;
OutputStream output = null;

//得到post方法
PostMethod postMethod = new PostMethod(path);

//设置post方法的参数
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name","lietu");
postData[1] = new NameValuePair("password","*****");
postMethod.addParameters(postData);

//执行,返回状态码
int statusCode = httpClient.executeMethod(postMethod);
//针对状态码进行处理,暂时处理200的状态
if(statusCode == HttpStatus.SC_OK)
{
input = postMethod.getResponseBodyAsStream();
//得到文件名
String fileName = path.substring(path.lastIndexOf('/')+1);
//获取文件输出流
output = new FileOutputStream(fileName);

//输出到文件
int tmpByte = -1;
while((tmpByte = input.read()) > 0)
{
output.write(tmpByte);
}
//关闭输入输出流
if(input != null)
{
input.close();
}
if(output != null)
{
output.close();
}
return true;
}
return false;
}


/*********
* 测试代码
*/
public static void main(String[] args) {
//抓取猎兔首页,输出
try {
RetrivePage.downloadPage("http://www.lietu.com");
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 0
原创粉丝点击