`

解析html

阅读更多
package com.web.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

public class WebTest
{

	public static void main(String[] args) throws IOException
	{
		//test1();
		//test2();
		test3();
		//test4();
	}
	
	public static void test1() throws IOException
	{
		
		URL url = new URL("http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg"); 
	    // 获得此URL的内容。
	    Object obj= url.getContent();
	    System.out.println(obj.getClass().getName());
		
	}
	public static void test2() throws IOException
	{
		URL url= new URL("http://quote.eastmoney.com/sh600007.html?StockCode=600007");
	    URLConnection uc=url.openConnection();
	    InputStream is=uc.getInputStream();
	    int c;
	    while((c=is.read())!=-1){
	    	System.out.println(c);
	    }
	    is.close();
	}
	public static void test3() throws IOException
	{
		URL url= new URL("http://quote.futures.hexun.com/EmbChart.aspx?code=CF1205&market=3");
	    Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream())); 
	    int c;
	    while((c=reader.read())!=-1){
	    	System.out.print((char)c);
	    }
	    reader.close();
	}
	//根据网络上的图片地址,将图片下载到本地
	public static void test4() throws IOException
	{
		File file=new File("E:/pic/1002.jpg");
		OutputStream out;
		URL url= new URL("http://hqpick.eastmoney.com/em_quote2007/PictureK.aspx?StockCode=000001&StockMarket=1&StockStyle=1&StockLayer=2&StockCurve=9");
	    InputStream is=url.openStream(); 
	    
	    file.createNewFile();
	    out=new FileOutputStream(file);
	    
	    int c;
	    while((c=is.read())!=-1){
	    	//System.out.print((char)c);
	    	out.write(c);
	    }
	    is.close();
	    out.close();
	    System.out.println("图片保存成功!");
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics