zoukankan      html  css  js  c++  java
  • 使用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是用于做文件续传 之类的 以后遇到再去研究吧……

  • 相关阅读:
    简单有效 四招教你保护好WiFi网络安全
    WP8手机安装《神庙逃亡》的教程
    如何关闭iOS7中的iPhone广告跟踪系统
    如何隐藏任务栏图标
    MVC中Controller和Action讲解上篇
    node.js
    express
    node.js+socket.io安装
    自定义view文字垂直居中
    AndroidTouch事件总结
  • 原文地址:https://www.cnblogs.com/guanxiaohe/p/12883452.html
Copyright © 2011-2022 走看看