提取网页中的源代码,以html格式存储

来源:互联网 发布:淘宝商品删除 重新上架 编辑:程序博客网 时间:2024/05/22 07:51
import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class UrlTest {public static void main(String args[]) throws IOException{URL url=new URL("http://www.baidu.com");URLConnection urlconnection=url.openConnection();InputStream is=urlconnection.getInputStream();<span style="white-space:pre"></span>//上面两行可以换为:InputStream is=url.openStream();<span style="white-space:pre"></span>OutputStream os=new FileOutputStream("c:/out.html");byte[] by=new byte[1024];int length=0;while(-1!=(length=is.read(by, 0,by.length))){os.write(by, 0, length);}is.close();os.close();}}

0 0
原创粉丝点击