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是用于做文件续传 之类的 以后遇到再去研究吧……

  • 相关阅读:
    ubuntu修改文件访问权限
    ubuntu使用root账户登录
    centos7 列出所有系统服务
    virtualbox 虚拟机硬盘扩容
    CI的意思
    更改centos 7 的默认启动为命令界面
    git Staging Deleted files
    Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
    List of data structures:数据结构大全
    List of algorithms:算法大全
  • 原文地址:https://www.cnblogs.com/guanxiaohe/p/12883452.html
Copyright © 2011-2022 走看看