java根据图片的url地址下载图片到本地

直接上代码:

 1 String str = "http://192.168.1.158/estun_cs/banner_img/head_pic.jpg";
 2 URL url = new URL(str);
 3 InputStream inputStream = null;
 4 try {
 5     inputStream = url.openStream();
 6 
 7 }catch (Exception e){
 8     e.printStackTrace();
 9 }
10 byte[] bytes = IOUtils.toByteArray(inputStream);
11 File imageFile = new File("/Users/edz/Desktop/NEW2/" + "test.png");
12 //创建输出流
13 FileOutputStream outStream = new FileOutputStream(imageFile);
14 //写入数据
15 outStream.write(bytes);
16 //关闭输出流
17 outStream.close();

 

posted @ 2018-05-08 16:11  低调的小白  阅读(842)  评论(0编辑  收藏  举报