using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using Math= System.Math;
using Uri = System.Uri;
using Convert= System.Convert;
public class DownloadTest : MonoBehaviour
{
public UILabel label1;
public UILabel label2;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
if (GUI.Button (new Rect (0, 0, 100, 20), "Download")) {
StartCoroutine (downfile ("http://localhost:8888/test.zip", Application.streamingAssetsPath + "/test.zip", label1));
}
if (GUI.Button (new Rect (0, 20, 100, 20), "StopDownload")) {
StopAllCoroutines ();
}
if (GUI.Button (new Rect (0, 40, 100, 20), "ResumDownload")) {
StartCoroutine (FPointDown ("http://localhost:8888/test.zip", Application.streamingAssetsPath + "/test.zip", label2));
}
}
string t = "";
//整体下载
IEnumerator downfile (string url, string LocalPath, UILabel DesLable)
{
Uri u = new Uri (url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create (u);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse ();
Stream sIn = wr.GetResponseStream ();
FileStream fs = new FileStream (LocalPath, FileMode.Create, FileAccess.Write);
long length = wr.ContentLength;
long i = 0;
decimal j = 0;
while (i<length) {
byte[] buffer = new byte[1024];
i += sIn.Read (buffer, 0, buffer.Length);
fs.Write (buffer, 0, buffer.Length);
if ((i % 1024) == 0) {
j = Math.Round (Convert.ToDecimal ((Convert.ToDouble (i) / Convert.ToDouble (length)) * 100), 4);
t = "当前下载文件大小:" + length.ToString () + "字节当前下载大小:" + i + "字节下载进度" + j.ToString () + "%";
DesLable.text = t.ToString ();
} else {
t = "当前下载文件大小:" + length.ToString () + "字节当前下载大小:" + i + "字节";
DesLable.text = t.ToString ();
}
yield return false;
}
sIn.Close ();
wr.Close ();
fs.Close ();
}
string downloadString = "已经下载";
//断点下载
IEnumerator FPointDown (string uri, string saveFile, UILabel DesLable)
{
//打开网络连接
HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create (uri);
HttpWebRequest requestGetCount = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create (uri);
long countLength = requestGetCount.GetResponse ().ContentLength;
//打开上次下载的文件或新建文件
long lStartPos = 0;
System.IO.FileStream fs;
if (System.IO.File.Exists (saveFile)) {
fs = System.IO.File.OpenWrite (saveFile);
lStartPos = fs.Length;
if (countLength - lStartPos <= 0) {
fs.Close ();
t = "已经";
DesLable.text = t.ToString ();
yield break;
}
fs.Seek (lStartPos, System.IO.SeekOrigin.Current);//移动文件流中的当前指针
} else {
fs = new FileStream (saveFile, FileMode.Create);
}
if (lStartPos > 0) {
request.AddRange ((int)lStartPos);//设置Range值
print (lStartPos);
}
//向服务器请求,获得服务器回应数据流
Stream ns = request.GetResponse ().GetResponseStream ();
int len = 1024 * 8;
byte[] nbytes = new byte[len];
int nReadSize = 0;
nReadSize = ns.Read (nbytes, 0, len);
while (nReadSize>0) {
fs.Write (nbytes, 0, nReadSize);
nReadSize = ns.Read (nbytes, 0, len);
t = downloadString + ":" + fs.Length / 1024 + "kb/" + countLength / 1024 + "kb" + "----" + ((double)fs.Length / countLength).ToString () + "%";
DesLable.text=t;
yield return false;
}
ns.Close ();
fs.Close ();
//这里放更新安装代码,或者可以测试这个下载的包有没有出错,验证sha和md5
}
}
unity3d 关于断点下载和整个下载(用于更新)
最新推荐文章于 2020-05-23 12:27:05 发布