Android从服务器下载文件
来源:互联网 发布:淘宝水印logo在线制作 编辑:程序博客网 时间:2024/05/17 22:07
//下载文件到sdcard,并自动进入软件安装界面private void downloadNewAPP() { new Thread(){ public void run() { InputStream in = null; FileOutputStream out = null; try { //创建httpclient HttpClient client = new DefaultHttpClient(); //设置超时时长 client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); //请求方式 HttpGet request = new HttpGet(path); //执行请求 HttpResponse response = client.execute(request); //判断响应码 if (response.getStatusLine().getStatusCode() == 200) { //获取apk流对象 in = response.getEntity().getContent(); //创建输出流 File file = new File(Environment.getExternalStorageDirectory(),getFileName(path)); out = new FileOutputStream(file); //下载 byte[] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer))!=-1){ out.write(buffer, 0, len); } //弹出安装界面 //创建message对象 Message msg = Message.obtain(); //设置类型 msg.what = INSTALL_NEW_VERSION; //发送消息 handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } finally { //释放流 try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } }; }.start();}//获取文件名private String getFileName(String path) { return path.substring(path.lastIndexOf("/"));}
0 0
- Android 从服务器下载文件
- Android 从服务器下载文件
- Android 从服务器下载文件
- Android从服务器下载文件
- Android连接服务器,从服务器获取数据,以及从服务器下载文件(单,多线程)
- android从服务器下载文件(php+apache+win7+MySql)
- Android 从服务器下载文件保存到sd卡
- 从FTP服务器下载文件
- php从服务器下载文件
- 从linux服务器下载文件
- VC 从服务器下载文件
- 从ftp服务器下载文件
- IOS 从服务器下载文件
- 从服务器下载word文件
- Servlet从服务器下载文件
- 从远程服务器下载文件
- java从服务器下载文件
- C#从服务器下载文件
- 常用shell命令
- 【Java】素数 解析与实现
- 【选择图片Jar】开篇预警(一)
- 开发工具
- 机构统计
- Android从服务器下载文件
- js 转json格式的字符串为对象或数组(前后台)
- Quicklook 以及 QLPreviewController
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- 《海量小文件》分布式文件系统-TFS
- 1/29
- JQuery onload、ready概念介绍及使用方法
- java获取系统硬件信息的第三方jar包