- 题目:通过URL/UrlConnetion编程,将百度首页下载到本地,存储文件baidu.txt中
- 代码实现
package com.czk.test.io; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; /** * 下载任意URL对应的网页html代码 * * @author czk * * @time:2019年8月2日 */ public class DownloadWebPage4UrlConnection { public static String download(String httpUrl) throws IOException { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.connect(); BufferedInputStream bis = null; ByteOutputStream bos = null; try { InputStream is = connection.getInputStream(); bis = new BufferedInputStream(is); bos = new ByteOutputStream(); int b = 0; byte[] byArr = new byte[1024]; while ((b = bis.read(byArr)) != -1) { bos.write(byArr, 0, b); } return new String(bos.toByteArray(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } public static void main(String[] args) throws IOException { String httpUrl = "http://www.baidu.com"; String content=download(httpUrl); System.out.println(content); } }
Java之下载相关网页到本地
最新推荐文章于 2024-08-18 03:08:17 发布