Unity Http下载文件断点续传

下载一个文件中断时,下次启动继续沿着上一次下载在很多游戏的更新中会用到

断点续传的核心代码API:

HttpWebRequest request = WebRequest.Create(tNowDownloadInfo.nNowDownloadURL) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.ServicePoint.ConnectionLimit = int.MaxValue;
//使用流操作文件
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
//获取文件现在的长度
long tmpLoaclFileLength = fs.Length;
//获取下载文件的总长度
long tmpTotlaSize = GetLength(tNowDownloadInfo.nNowDownloadURL, tNowDownloadInfo.nDownloadTimeout);
//断点续传重要API,设置本地开始下载起始位置
fs.Seek(tmpLoaclFileLength, SeekOrigin.Begin);

如果使用带https头的则需要添加设置协议:

if (tNowDownloadInfo.nNowDownloadURL.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
    ServicePointManager.ServerCertificateValidationCallback = new                             
    RemoteCertificateValidationCallback(ValidateServerCertificate);
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
}
else
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
}



static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
   return true; //接受     
}

完整代码:

using UnityEngine;
using System.Collections;
using System.Threading;
using System.IO;
using System.Net;
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;

public enum DownloadResult
{
    /// <summary>
    /// 当前下载的某一个文件已下载成功,不是指一个完全的下载任务已完成
    /// </summary>
    Success,
    /// <summary>
    /// 某一个下载文件下载失败
    /// </summary>
    Fail,
    /// <summary>
    /// 获取不到文件
    /// </summary>
    NotFindFile,
    /// <summary>
    /// 下载文件中
    /// </summary>
    Downloading,
    /// <summary>
    /// 完成一个下载任务
    /// </summary>
    AllDownloadFinished,
}

public class DownloadInfo
{
    /// <summary>
    /// 是否可以执行回调,用于解决线程之间的问题
    /// </summary>
    public volatile bool tIsCanCallback;
    /// <summary>
    /// 当前下载的任务文件大小总和
    /// </summary>
    public int nAllFileTotalSize = 1;
    public DownloadResult tDownloadState;
    public float tProgress = 0;
    public bool tIsDone = false;
    public DownlaodFileCallback tDownloadCallback;
    public string[] tSavaeFullFilePath;
    public string[] tDownlaodURL;
    /// <summary>
    /// 当前正在下载的文件地址
    /// </summary>
    public string nNowDownloadURL = "";
    /// <summary>
    /// 当前下载完成保存本地的地址
    /// </summary>
    public string nSavaeFullFilePath = "";
    /// <summary>
    /// 当前下载索引
    /// </summary>
    public int nDownloadIndex = 0;
    /// <summary>
    /// 每秒的下载速度
    /// </summary>
    public int nTimeDownSpeed = 0;
    /// <summary>
    /// 每隔多少时间执行回调一次
    /// </summary>
    public float nUpdateExeCallTime = 1f;
    /// <summary>
    /// 当前总下载的进度
    /// </summary>
    public long nLocalFileTotalSize = 0;
    /// <summary>
    /// 当前下载的文件总大小
    /// </summary>
    public int nNowTotalSize = 0;

    public int nDownloadTimeout = 6000;
    /// <summary>
    /// 下载的数据是否保存到本地,如果需要设置的话请保证访问的文件小于2MB
    /// </summary>
    public bool nIsSavaeFile = true;
    /// <summary>
    /// 下载的数据,只有当nIsSavaeFile false的时候数据会存在该字段中
    /// </summary>
    public byte[] nToFileData;
}

public delegate void DownlaodFileCallback(DownloadInfo varInfo, DownloadResult varResultType);
/// <summary>
/// 通过http下载资源
/// </summary>
public class HttpDownload : MonoBehaviour
{
    static HttpDownload Instance;
    public static HttpDownload GetInstacne()
    {
        if (Instance == null)
        {
            GameObject tHttpDownLoad = new GameObject("
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值