android将InputStream转为FileOutputStream保存HttpURLConnection获取到的数据
2013-04-16 16:22
465 查看
有时候我们需要进行网络连接下载指定连接的数据。例如是一个更新的APK文件,我们需要将它下载并保存。
以下代码可以参考:
以下代码可以参考:
try { URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(10 * 1000); connection.connect(); if (connection.getResponseCode() == HttpStatus.SC_OK) { File file = new File("/mnt/sdcard/fucking/dd.apk"); file.createNewFile(); InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 10]; while (true) { int len = inputStream.read(buffer); publishProgress(len); if (len == -1) { break; } arrayOutputStream.write(buffer, 0, len); } arrayOutputStream.close(); inputStream.close(); byte[] data = arrayOutputStream.toByteArray(); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(data); fileOutputStream.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
相关文章推荐
- Android通过HttpURLConnection链接到网络,并获取网络数据
- Android HttpURLConnection数据获取并JSON解析
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android使用HttpURLConnection获取数据
- 使用HttpUrlConnection获取数据,Unicode和GZIPInputStream的坑
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- Android2.2以上的版本HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不相等
- Android 4.0 HttpUrlConnection的getInputStream()方法总是返回空的问题
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- 【Android小练手】安卓端实时获取HttpUrl数据
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android使用HttpURLConnection请求网络返回JSON数据并解析
- 使用 HttpURLConnection 获取不到网络数据
- Android客户端使用HttpURLConnection doGet与服务器数据交互简单示例
- HttpURLConnection获取网络数据(实例:网路请求图片)
- android中的HttpURLConnection和HttpClient实现app与pc数据交互
- Android之HttpURLConnection应用实例:武大教务系统获取课表
- Android用httpURLConnection发送post网络请求并拿到数据