根据url下载图片

如题:在我要动手写的时候才发现不搜索根本就是写不出来,究其原因还是因为基础不扎实,由于用的少已经没有能力写出了

首先需要获取url数据流,然后写进文件里即可,仅仅两步可惜我写不出来啊跟着搜来的内容写一下

public static void downLoadPic(String url,String fileName,String savePath) throws IOException{
		URL u=new URL(url);// 构造URL
		URLConnection con=u.openConnection();
		//设置请求路径
		con.setConnectTimeout(5*1000);
		//输入流
		InputStream is=con.getInputStream();
		byte[] b=new byte[1024];
		//读取到的数据长度
		int len;
		//输出的文件流
		File sf=new File(savePath+fileName);
		if(!sf.exists()){
		
		}
		OutputStream os=new FileOutputStream(sf);
		//开始读取
		while((len=is.read(b))!=-1){
			os.write(b, 0, len);
		}
		os.close();
		is.close();
		
	}

  

原文地址:https://www.cnblogs.com/shuiliuhualuo/p/6050721.html