java下载html页面---把网页内容保存成本地html
来源:互联网 发布:设置淘宝子账号 编辑:程序博客网 时间:2024/05/22 03:31
我们在前面讲到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);// 连接时间20shttpPostClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 8000);// 数据传输时间60sif (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即可
- public static void writeToFile(String fileName, String content) {
- String time = DATE_FORMAT.format(Calendar.getInstance().getTime());
- File dirFile = null;
- try {
- dirFile = new File("e:\\" + time);
- if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
- boolean creadok = 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
- */
- public static boolean write(String path, String content) {
- String s = new String();
- String s1 = new String();
- BufferedWriter output = null;
- try {
- File f = new File(path);
- if (f.exists()) {
- } else {
- System.out.println("文件不存在,正在创建...");
- if (f.createNewFile()) {
- System.out.println("文件创建成功!");
- } else {
- System.out.println("文件创建失败!");
- }
- }
- BufferedReader input = new BufferedReader(new FileReader(f));
- while ((s = input.readLine()) != null) {
- s1 += s + "\n";
- }
- System.out.println("原文件内容:" + s1);
- input.close();
- s1 += content;
- output = new BufferedWriter(new FileWriter(f));
- output.write(s1);
- output.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (output != null) {
- try {
- output.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
0 0
- java下载html页面---把网页内容保存成本地html
- java下载保存html页面
- 动态页面静态化----将jsp转换成本地html
- java获取网页html内容。。。。。。。
- 下载HTML内容保存到文件
- JS实现页面HTML内容以另存为的文件形式下载保存
- 如何用java实现在网页中保存html编辑器 编辑好的 html 文件?!(如blog里发文章的时候把要发表的内容以html文件的形式保存到服务器上?)
- php把采集内容中图片地址下载并替换成本地地址
- js如何把html页面中指定的内容打印出来(下载下来)?
- python 保存网页HTML
- XML保存HTML页面的下拉列表内容
- Web页面截图或HTML内容保存为图片
- java-jsoup解析html页面的内容
- java-jsoup解析html页面的内容
- 获取html页面内容
- HTML页面内容
- 把html页面的部分内容保存成新的html文件
- 把html页面的部分内容保存成新的html文件的jquery代码
- android数据转换tips
- WriteHuge 不是CFile的成员
- iOS protubuf 的使用
- Android应用的核心基础
- 开源监控利器nagios实战
- java下载html页面---把网页内容保存成本地html
- 使用 Eclipse 的 SVN 插件创建项目的主干/分支/标签
- Java中request获取IP(转自推酷)
- soap本地测试
- This Android SDK requires Andriod Developer Toolkit version 20.0.0 or above
- Android自定义LinLayout
- 微信公众平台开发之相同关键词不同回复-php语言(五)
- POJ - 3039 MARGARITAS ON THE RIVER WALK
- Android签名之生成keystore的两种方式