HttpClient获得内容写到文件
2014-11-04 11:39
162 查看
package com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class HttpClientToIOFile {
public
static void main(String[] args) throws FileNotFoundException
{
HttpClient client = new HttpClient();
GetMethod method = new
GetMethod("http://www.baidu.com");
InputStream is = null;
StringBuilder sb = new StringBuilder();
//http.txt 不存在 自动创建
FileOutputStream fo = new
FileOutputStream("R:\\http2.txt", true);
try {
int code = client.executeMethod(method);
if (code == HttpStatus.SC_OK) {
System.out.println("SUCCESS:" + code);
is = method.getResponseBodyAsStream();
int len = 0;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) > 0) {
sb.append(new String(buf, 0, len));
fo.write(sb.toString().getBytes());
}
System.out.println(sb.toString());
} else {
System.out.println("ERROR:" + code);
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class HttpClientToIOFile {
public
static void main(String[] args) throws FileNotFoundException
{
HttpClient client = new HttpClient();
GetMethod method = new
GetMethod("http://www.baidu.com");
InputStream is = null;
StringBuilder sb = new StringBuilder();
//http.txt 不存在 自动创建
FileOutputStream fo = new
FileOutputStream("R:\\http2.txt", true);
try {
int code = client.executeMethod(method);
if (code == HttpStatus.SC_OK) {
System.out.println("SUCCESS:" + code);
is = method.getResponseBodyAsStream();
int len = 0;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) > 0) {
sb.append(new String(buf, 0, len));
fo.write(sb.toString().getBytes());
}
System.out.println(sb.toString());
} else {
System.out.println("ERROR:" + code);
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
相关文章推荐
- Java URLConnection 类进行http连接,获得文件内容
- 使用HttpClient获得网页内容
- XMLHTTP的应用,获得远程的文件,获得远程HTML文件源码
- 使用TextMiniing和Apache POI获得Word文件内容,无须MS-Office ActiveX
- dz asp.net论坛中函数--根据Url获得源文件内容
- HttpClient远程发送文件
- 将多个文件写到一个文件(并合文件),也可从该并合文件中获得其中某个文件的流(具体不多写了,压缩功能未加进,有兴趣留言)
- 关于httpclient上传文件是中文名称的处理
- C# 获得.xls文件的内容 获得.xls文件中的表名
- httpclient从一个服务器传送文件到另一个服务器
- 通过http/https的POST方式,发送和接受XML文件内容
- 通过http/https的POST方式,发送和接受XML文件内容
- XMLHTTP的应用,获得远程的文件,获得远程HTML文件源码
- 在oracle中通过utl_http获得指定网页内容
- C# Visual Studio 2005中将一个TextBox中的内容通过FileStream写到文件中
- 使用commons-httpclient上传文件
- 使用Apache POI和Java获得Excel文件内容,无须MS-Office ActiveX OLE
- 获得表的字段并将内容写入XML文件中
- Java基础:利用HttpClient获取网页内容
- 通过http/https的POST方式,发送、处理和接收XML文件内容