欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

安卓下载保存到本地(一)

发布时间:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 安卓下载保存到本地(一) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
public void getFileFromServer(String path) throws Exception{
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(path);
HttpResponse response;
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {


File file = new File(
Environment.getExternalStorageDirectory(),
fileName);
fileOutputStream = new FileOutputStream(file);


byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;

 
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (is != null) {
is.close();
}
}
创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的安卓下载保存到本地(一)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。