使用HttpClient下载文件出现的206问题

写爬虫的时候需要下载视频 遇到206问题

下载报错前的代码

try {
            HttpResponse resp = httpclient.execute(httpget);
            if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
                HttpEntity entity = resp.getEntity();
                InputStream in = entity.getContent();
                savePicToDisk(in, dirPath, fileName);
                logger.info("下载文件 " + fileName + " 成功....");
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("下载文件 "+fileName+" 失败....");
        } finally {
            httpclient.getConnectionManager().shutdown();
        }

修改后代码

try {
            HttpResponse resp = httpclient.execute(httpget);
            if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode() || HttpStatus.SC_PARTIAL_CONTENT == resp.getStatusLine().getStatusCode()) {
                HttpEntity entity = resp.getEntity();
                InputStream in = entity.getContent();
                savePicToDisk(in, dirPath, fileName);
                logger.info("下载文件 " + fileName + " 成功....");
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("下载文件 "+fileName+" 失败....");
        } finally {
            httpclient.getConnectionManager().shutdown();
        }

看到这里懂了吧 其实在文件下载 把206当做200一样处理就行了 

似乎206是用于做文件续传 之类的 以后遇到再去研究吧……

原文地址:https://www.cnblogs.com/guanxiaohe/p/12883452.html