您的位置:首页 > 移动开发 > Android开发

android 文件下载 超简单

2016-11-22 21:25 281 查看
public void downloadPlug(String downloadUrl,String savePath) {
try {
URL url = new URL(downloadUrl);
//打开连接
URLConnection conn = url.openConnection();
//打开输入流
InputStream is = conn.getInputStream();
//获得长度
int contentLength = conn.getContentLength();

//下载后的文件名
File downLoadFile = new File(savePath);
if (downLoadFile.exists()) {
downLoadFile.delete();
}
//创建字节流
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(downLoadFile);
//写数据
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
//完成后关闭流
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: