Android 文件下载302的问题,下载地址重定向

在安卓开发中经常需要用到文件下载功能,很多的时候会用基础的写法去写,比如

 
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;
}

这样无论地址是否是最终的文件地址,就都可以访问到文件地址了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闽农qq:994955138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值