根据URL链接下载图片

根据URL下载图片

/*
 urlList:需要保存图片的名称(我这里是截取的,但方法里其实没用到)
 pic_url:需要下载图片的链接
 path:路径名
*/
	public static void downloadPicture(String urlList, String pic_url,String path) {
		// TODO Auto-generated method stub
		 try {	
			 URL url=new URL(pic_url);
			 urlList = urlList.substring(urlList.indexOf("mall_id")+8,urlList.indexOf("&"));
			 System.out.println(urlList);
			  DataInputStream dataInputStream = new DataInputStream(url.openStream());//读取网络资源
	          FileOutputStream fileOutputStream = new FileOutputStream(path);//写入文件
	          ByteArrayOutputStream output = new ByteArrayOutputStream();//创建一个字节数组缓冲区
	            byte[] buffer = new byte[1024];
	            int length;
	            while ((length = dataInputStream.read(buffer)) > 0) {
	                output.write(buffer, 0, length);
	            }
	            fileOutputStream.write(output.toByteArray());
	            dataInputStream.close();
	            fileOutputStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

测试

   public static void main(String[] args)
       {
    	   String url="http://mobile.yangkeduo.com/mall_certificates.html?mall_id=466839685&refer_page_name=login&refer_page_id=login_1539132109994_s1IGaeoxzZ&refer_page_sn=10039";
    	   String pic_url="http://imsproductionimg.yangkeduo.com/ded2dacde195436abe27a7e4ab6a9cbd.jpg?x-oss-process=image/watermark,text_5ou85aSa5aSa5YWl6am75LiT55SoIOWFtuS7luaXoOaViA==,type_ZmFuZ3poZW5nc2h1c29uZw==,color_c8c8c8,size_12,shadow_0,rotate_345,fill_1";
  		   String path = "E:\\images\\"+url.substring(url.indexOf("mall_id")+8,url.indexOf("&"))+"_营业执照(文字).jpg";
    	   downloadPicture(url,pic_url,path);           
       }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值