根据URL下载文件

//创建链接
URL url = new URL("http://api.map.baidu.com/images/weather/day/xiaoyu.png");

//打开链接
URLConnection conn = url.openConnection();
conn.setConnectTimeout(1000);
InputStream is = conn.getInputStream();
byte[] bs = new byte[1024];
int len;
File f = new File("d:/");
if(!f.exists()){
     f.mkdir();
}
OutputStream os = new FileOutputStream(f.getPath()+"\\"+ UUID.randomUUID() + ".png");
while((len = is.read(bs)) != -1){
     os.write(bs, 0, len);
}
is.close();
os.close();

 

posted @ 2018-01-11 10:18  没有听见下雨的声音  阅读(238)  评论(0编辑  收藏  举报