URL:下载资源

版权声明:版权所有@万星明 https://blog.csdn.net/qq_19533277/article/details/83714035
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/** 
* @author  万星明
* @version 创建时间:2018年10月26日 上午9:33:31 
*/
public class URL下载资源 {
	public static void main(String[] args) throws Exception {
		
		//创建一个URL对象
		URL url = new URL("");
		//创建网络读取流
		InputStream  is = url.openStream();
		//创建本地写入流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//边读边写
		 byte[] b = new byte[1024];
		 int len = is.read(b);
		 while(len!=-1) {
			 baos.write(b,0,len);
			 len = is.read(b);
		 }
		//将写入内存中的信息,一次性写入到本地文件中
		 baos.writeTo(new FileOutputStream("a.jpg"));
		 System.out.println("下载成功");
		 //关流
		 is.close();
		 baos.close();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_19533277/article/details/83714035