您的位置:首页 > 编程语言 > Java开发

Java常用代码

2017-11-08 17:18 316 查看
1. 下载网络图片

public void download(String strUrl, String filename){
try{
URL url = new URL(strUrl);
HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.connect();
InputStream in = conn.getInputStream();

OutputStream out = new FileOutputStream(new File(filename));

byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf)) != -1){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println(filename + "下载成功");
}catch(Exception e){
System.out.println(filename + "下载失败");
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: