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

来源:互联网 发布:淘宝店被扣48分怎么办 编辑:程序博客网 时间:2024/06/11 18:51
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();
}
}
0 0
原创粉丝点击