博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]java 下载网络上的图片并保存到本地目录
阅读量:4694 次
发布时间:2019-06-09

本文共 1432 字,大约阅读时间需要 4 分钟。

原文地址:

1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.net.URL; 6 import java.net.URLConnection; 7  8  9 public class DownloadImage {10 11     /**12      * @param args13      * @throws Exception 14      */15     public static void main(String[] args) throws Exception {16         // TODO Auto-generated method stub17          download("http://ui.51bi.com/opt/siteimg/images/fanbei0923/Mid_07.jpg", "51bi.gif","c:\\image\\");18     }19     20     public static void download(String urlString, String filename,String savePath) throws Exception {21         // 构造URL22         URL url = new URL(urlString);23         // 打开连接24         URLConnection con = url.openConnection();25         //设置请求超时为5s26         con.setConnectTimeout(5*1000);27         // 输入流28         InputStream is = con.getInputStream();29     30         // 1K的数据缓冲31         byte[] bs = new byte[1024];32         // 读取到的数据长度33         int len;34         // 输出的文件流35        File sf=new File(savePath);36        if(!sf.exists()){37            sf.mkdirs();38        }39        OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);40         // 开始读取41         while ((len = is.read(bs)) != -1) {42           os.write(bs, 0, len);43         }44         // 完毕,关闭所有链接45         os.close();46         is.close();47     } 48 49 }

 

转载于:https://www.cnblogs.com/dirgo/p/7428596.html

你可能感兴趣的文章
我是怎么用缠论在商品里边抢钱之二 (2019-07-12 15:10:10)
查看>>
python入门之正则表达式
查看>>
SAS学习经验总结分享:篇五-过程步的应用
查看>>
Android创建文件夹及文件并写入数据
查看>>
file的getPath getAbsolutePath和getCanonicalPath的不同
查看>>
课时4—切入切出动画
查看>>
eclipse 编辑 python 中文乱码的解决方案
查看>>
Python 爬虫的集中简单方式
查看>>
数据库MySQL/mariadb知识点——触发器
查看>>
Ubuntu做Tomcat服务:insserv: warning: script 'tomcat' missing LSB tags and overrides
查看>>
Binary Agents
查看>>
入门Webpack,看这篇就够了
查看>>
短信拦截马”黑色产业链与溯源取证研究
查看>>
Mac Xdebug安装时遇到了Zend Engine API 不一致的问题
查看>>
最小公倍数
查看>>
asp.net如何定时执行任务
查看>>
在github上实现页面托管预览功能
查看>>
css选择器
查看>>
prim
查看>>
给陌生人写一封信
查看>>