HttpURLConnection中econnreset异常之后的理解
2016-03-31 09:53
501 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014194661/article/details/51023535
/*** 文件下载 */ class downloadTask extends Thread { private String downloadUrl;// 下载链接地址 private String filePath;// 保存文件路径地址 private int position; //确定下载列表中的索引号 public downloadTask(String downloadUrl, String fileptah,int position) { this.downloadUrl = downloadUrl; this.filePath = fileptah; this.position = position; } @Override public void run() { try { URL url = new URL(downloadUrl); //LogHelper.e(TAG, "download file http path:" + position +" | " + downloadUrl); LogHelper.e("线程", this.hashCode() + "|" + downloadUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(40*1000) ; conn.setRequestProperty("Charset", "UTF-8"); //设置为长连接 conn.setRequestProperty("Connection", "Keep-Alive"); conn.setAllowUserInteraction(true); conn.setReadTimeout(40*1000); // 读取下载文件总大小 int fileSize = conn.getContentLength(); if (fileSize <= 0) { System.out.println("读取文件失败"); return; } // 设置ProgressBar最大的长度为文件Size holderList.get(position).PBDOWNLOAD.setMax(fileSize); synchronized(this){ BufferedInputStream bis = null; byte[] buffer = new byte[1024]; bis = new BufferedInputStream(conn.getInputStream()); LogHelper.e("线程", this.hashCode() + "|" +bis.hashCode() ); FileOutputStream fos = new FileOutputStream(filePath, true); /** 当前下载文件长度 */ int downloadLength = 0; int len; while ((len = bis.read(buffer, 0, 1024)) != -1) { fos.write(buffer, 0, len); downloadLength += len; // 通知handler去更新视图组件 Message msg = new Message(); msg.getData().putInt("size", downloadLength); msg.getData().putInt("position", position); downloadHandler.sendMessage(msg); } //关闭文件IO连接 if(fos != null){ fos.close(); } //关闭网络流 if(bis != null){ bis.close(); } //关闭http连接 if(conn != null){ conn.disconnect(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); LogHelper.e("aaaaaaaa", e.getMessage()); } } }
注意事项:
1、关闭连接和流
2、conn.setReadTimeout(40*1000); 当网速不好,或者多个线程抢占cpu,导致下载文件在一定时间里没有执行下载操作,服务器以为客户端已经断开连接,服务器会关闭连接,导致程序下载出错。因此考虑到特殊情况,可以适当的把conn.setReadTimeout(40*1000)设置长一点。conn.setReadTimeout(40*1000);
conn.setReadTimeout(40*1000);
相关文章推荐
- 对 URLConnection、HttpURLConnection 与 HttpClient 的理解
- URL、URLConnection、HttpURLConnection理解
- Android---------HttpURLConnection的 get 请求和 post 请求(更容易理解)
- faultDetail = "NetConnection.Call.Failed: HTTP: Status 404: url: 'http://localhost...
- Android学习之使用HttpURLConnection同时上传文本和多个文件
- httpURLConnection-网络请求的两种方式-get请求和post请求
- Android4.0以上HttpURLConnection出FileNotFoundException异常解决
- java 通过Ajax前台传参数 并用 HttpURLConnection Post方式访问对外的接口
- 利用HttpURLConnection对象和Internet交互
- Android网络编程(二)HttpClient与HttpURLConnection
- 【Android基础知识】HttpUrlConnection使用doGet和doPost方式和服务器交互
- Java HttpURLConnection 联网超时问题
- HTTPURLConnection 发送Post数据
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- 如何通过HttpURLConnection得到http 302的跳转地址
- HttpURLConnection 访问服务器,GET和POST方法.
- HttpURLConnection用法详解
- HttpClient和HttpURLConnection的区别
- HttpURLConnection(一)
- Android网络连接之HttpURLConnection和HttpClient