从网页中提取图片到本地

package ttest;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class jietu {
	private static URLConnection open;
	private static InputStream in;

	public static void main(String[] args) throws Exception {
		// TODO 自动生成的方法存根
//		1.打开外部网页	
		String url = "http://p0.so.qhmsg.com/t0193ef441033819585.jpg";
//		2.建立连接
		URL url1 = new URL(url);
		open = url1.openConnection();//打开连接
		open.connect();
//		3.下载资源
		in = open.getInputStream();
//		4.将资源写入本地磁盘
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:/beauty.jpg"));
		byte[] b = new byte[2048];
		int a  = -1;
		while((a=in.read(b))!=-1){
			out.write(b, 0, a);	
		
		}
//		5.关闭流
		out.flush();
		in.close();
		out.close();

	}

}

你可能感兴趣的:(从网页中提取图片到本地)