在安卓开发中经常需要用到文件下载功能,很多的时候会用基础的写法去写,比如
URL httpUrl = new URL(url.replaceAll(" ", "%20")); HttpURLConnection http = (HttpURLConnection) httpUrl.openConnection(); http.setConnectTimeout(5 * 1000); http.setRequestMethod("GET"); http.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); //设置客户端可以接受的返回数据类型 http.setRequestProperty("Accept-Language", "zh-CN"); //设置客户端使用的语言问中文 http.setRequestProperty("Charset", "UTF-8"); //设置通信编码为UTF-8 http.setRequestProperty("Accept-Connection", "identity"); http.setRequestProperty("Range", "bytes=" + startPos + "-"); http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); //客户端用户代理 http.setRequestProperty("Connection", "Keep-Alive"); //使用长连接 inStream = http.getInputStream();
用这种写法,在url是文件的直接地址时没有问题。但是,当url是带有统计或者其他功能的地址,在我们访问时被转发了之后。http.getResponseCode()就会返回302。并且http.getContentLength()为0。这是因为我们没有访问到文件的真正地址。所以需要url重定向,inStream = http.getInputStream();if(http.getResponseCode() == 302){ inStream = reconnection(http.getHeaderField("Location"),startPos, infoBean); }private InputStream reconnection(String url,long startPos, DownloadGameInfoBean infoBean){ try { URL httpUrl = new URL(url.replaceAll(" ", "%20")); HttpURLConnection http = (HttpURLConnection) httpUrl.openConnection(); http.setConnectTimeout(5 * 1000); http.setRequestMethod("GET"); http.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); //设置客户端可以接受的返回数据类型 http.setRequestProperty("Accept-Language", "zh-CN"); //设置客户端使用的语言问中文 http.setRequestProperty("Charset", "UTF-8"); //设置通信编码为UTF-8 http.setRequestProperty("Accept-Connection", "identity"); http.setRequestProperty("Range", "bytes=" + startPos + "-"); http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); //客户端用户代理 http.setRequestProperty("Connection", "Keep-Alive"); //使用长连接 //http.getContentLength()获得从startPos开始到结尾的长度,所以要加上startPos才是总长度 infoBean.setEndPos(http.getContentLength() + startPos); return http.getInputStream(); }catch (Exception e){ e.printStackTrace(); } return null; }这样无论地址是否是最终的文件地址,就都可以访问到文件地址了