安卓下载保存到本地(一)
生活随笔
收集整理的这篇文章主要介绍了
安卓下载保存到本地(一)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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();
}
}
创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖
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();
}
}
创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖
总结
以上是生活随笔为你收集整理的安卓下载保存到本地(一)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 自定义 URL Scheme 完全指南
- 下一篇: 安卓下载保存到本地(二)