qt 断点续传例子

//功能:    根据一个URL地址将数据保存到指定路径下,支持断点续传
//参数:    url            --需要访问的URL地址
//         SavePath       --需要保存的路径
//DownedSize 已经下载的大小
// totalSize 文件总大小
//返回值:  ture --成功 false --失败
bool HttpGet::DownFile(const QUrl &url,const QString &SavePath,int DownedSize,int totalSize)
{ //创建父文件夹
  QString curPath=QApplication::applicationDirPath()+"/Files";
  if(!QDir(curPath).exists())
  {
   QDir photoDir;
   photoDir.mkdir(curPath);
  }
 //创建子文件夹
    if(!QDir(SavePath).exists())
    {
     QDir photoDir;
     photoDir.mkdir(SavePath);
    }
    QNetworkRequest qheader;
    qheader.setUrl(url);
    QString Range="bytes "+QString::number(DownedSize)+"-";//告诉服务器从DownedSize起开始传输
    qheader.setRawHeader("Range",Range.toAscii());

        QNetworkAccessManager manager;
        //参考 http://www.qtforum.org/article/31355/qnetworkaccessmanager-using-custom-headers-to-download-a-file.html
        QEventLoop loop;
        //QNetworkReply *reply = manager.get(QNetworkRequest(url));
        QNetworkReply *reply = manager.get(QNetworkRequest(qheader));
        QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        QFileInfo fileInfo=url.path();
        QFile file(SavePath+fileInfo.fileName());
        file.open(QIODevice::WriteOnly);
        file.write(reply->readAll());
        delete reply;
        return true;
}

使用

  getter.DownFile(QUrl(FileUrl),QString(CurrentPath),0,FileSize);
  QObject::connect(&getter, SIGNAL(finished()),  SLOT(quit()));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值