java 读取url html 保存_java下载html页面---把网页内容保存成本地html

本文介绍了如何使用Java从URL获取HTML内容,并将其保存到本地作为HTML文件。首先通过HttpGet和HttpClient获取页面内容,然后利用java.io包中的类将内容写入文件,实现网页保存。
摘要由CSDN通过智能技术生成

我们在前面讲到httpclient抓取网页内容的时候 通常都是获取到页面的源代码content存入数据库。

详见下文:

HTTPClient模块的HttpGet和HttpPost

httpclient常用基本抓取类

那么如果我们除了获得页面源代码之外 还想把页面保存到本地存成html应该怎么做呢?

其实很简单 我们先来看访问页面获取content的代码

private static String getUrlContent(DefaultHttpClient httpPostClient,

String urlString) throws IOException, ClientProtocolException {

HttpGet httpGet = new HttpGet(urlString);

HttpResponse httpGetResponse = httpPostClient.execute(httpGet);// 其中HttpGet是HttpUriRequst的子类

httpPostClient.getParams().setParameter(

CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);// 连接时间20s

httpPostClient.getParams().setParameter(

CoreConnectionPNames.SO_TIMEOUT, 8000);// 数据传输时间60s

if (httpGetResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpGetResponse.getEntity();

if (httpEntity.getContentEncoding() != null) {

if ("gzip".equalsIgnoreCase(httpEntity.getContentEncoding()

.getValue())) {

httpEntity = new GzipDecompressingEntity(httpEntity);

} else if ("deflate".equalsIgnoreCase(httpEntity

.getContentEncoding().getValue())) {

httpEntity = new DeflateDecompressingEntity(httpEntity);

}

}

String result = enCodetoString(httpEntity, encode);// 取出应答字符串

// System.out.println(result);

return result;

}

return "";

}

public static String enCodetoStringDo(final HttpEntity entity,

Charset defaultCharset) throws IOException, ParseException {

if (entity == null) {

throw new IllegalArgumentException("HTTP entity may not be null");

}

InputStream instream = entity.getContent();

if (instream == null) {

return null;

}

try {

if (entity.getContentLength() > Integer.MAX_VALUE) {

throw new IllegalArgumentException(

"HTTP entity too large to be buffered in memory");

}

int i = (int) entity.getContentLength();

if (i < 0) {

i = 4096;

}

Charset charset = null;

try {

// ContentType contentType = ContentType.get(entity);

// if (contentType != null) {

// charset = contentType.getCharset();

// }

} catch (final UnsupportedCharsetException ex) {

throw new UnsupportedEncodingException(ex.getMessage());

}

if (charset == null) {

charset = defaultCharset;

}

if (charset == null) {

charset = HTTP.DEF_CONTENT_CHARSET;

}

Reader reader = new InputStreamReader(instream, charset);

CharArrayBuffer buffer = new CharArrayBuffer(i);

char[] tmp = new char[1024];

int l;

while ((l = reader.read(tmp)) != -1) {

buffer.append(tmp, 0, l);

}

return buffer.toString();

} finally {

instream.close();

}

}

我们得到content之后就可以直接 把它存成本地文件 就 可以了。

我们可以参考

java读写txt

把txt后缀改成html即可

publicstaticvoidwriteToFile(String fileName, String content) {

String time = DATE_FORMAT.format(Calendar.getInstance().getTime());

File dirFile = null;

try{

dirFile = newFile("e:\\"+ time);

if(!(dirFile.exists()) && !(dirFile.isDirectory())) {

booleancreadok = dirFile.mkdirs();

if(creadok) {

System.out.println(" ok:创建文件夹成功! ");

} else{

System.out.println(" err:创建文件夹失败! ");

}

}

} catch(Exception e) {

e.printStackTrace();

}

String fullPath = dirFile + "/"+ fileName +".txt";

write(fullPath, content);

}

/**

* 写文件

*

* @param path

* @param content

*/

publicstaticbooleanwrite(String path, String content) {

String s = newString();

String s1 = newString();

BufferedWriter output = null;

try{

File f = newFile(path);

if(f.exists()) {

} else{

System.out.println("文件不存在,正在创建...");

if(f.createNewFile()) {

System.out.println("文件创建成功!");

} else{

System.out.println("文件创建失败!");

}

}

BufferedReader input = newBufferedReader(newFileReader(f));

while((s = input.readLine()) !=null) {

s1 += s + "\n";

}

System.out.println("原文件内容:"+ s1);

input.close();

s1 += content;

output = newBufferedWriter(newFileWriter(f));

output.write(s1);

output.flush();

returntrue;

} catch(Exception e) {

e.printStackTrace();

returnfalse;

} finally{

if(output !=null) {

try{

output.close();

} catch(IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值