1. 程式人生 > >網絡編程通過輸入輸出流下載圖片

網絡編程通過輸入輸出流下載圖片

圖片 ati () ring [] ont args .net origin

/**
 * 
 */
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!");
		
	}

}

  

網絡編程通過輸入輸出流下載圖片