网络编程通过输入输出流下载图片

/**
 * 
 */
package itest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author archer
 *
 */
public class UrlConnectionTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String urlString = "http://bpic.588ku.com/art_origin_min_pic/18/12/02/e0549633966e7115c28631964a8e8b6d.jpg";
		String path ="E:\Temp\2019.jpg";  
		int n = -1;
		URL url = new URL(urlString);
		URLConnection conn = url.openConnection();
		//获取输入流
		InputStream in = conn.getInputStream();
		//获取输出流
		OutputStream out = new FileOutputStream(path);
		//循环读写过程
		while((n = in.read()) != -1){
			out.write(n);
		}
		in.close();
		out.close();
		System.out.println("completed!");
		
	}

}

  

原文地址:https://www.cnblogs.com/archer-wen/p/10280173.html