网站建设托管产品图,广东全网营销与推广公司,wordpress怎么建立下载,为什么要建设营销型网站在这里我用了一个线程池#xff0c;线程池参数接收一个带有object参数的#xff0c;无返回值的委托 #xff0c;下载用到的核心代码#xff0c;网上拷贝的#xff0c;他的核心就是发起一个web请求#xff0c;然后得到请求的响应#xff0c;读取响应的流 剩下的都是常见的…在这里我用了一个线程池线程池参数接收一个带有object参数的无返回值的委托 下载用到的核心代码网上拷贝的他的核心就是发起一个web请求然后得到请求的响应读取响应的流 剩下的都是常见的IO操作 由于线程池接参数的委托接收一个object参数。所以我把请求地址和本地存放地址以及名字封装了一个类用来传参 这是下载工具代码如下 using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;public class DownLoaderEnum
{public string url;public string path;public DownLoaderEnum(string URL, string PATH){url URL;path PATH;}
}public class HttpDownload
{/// summary/// http下载文件/// /summary/// param nameurl下载文件地址/param/// param namepath文件存放地址包含文件名/param/// returns/returnspublic void HttpDownloader(object down){if (!Directory.Exists((down as DownLoaderEnum).path))Directory.CreateDirectory((down as DownLoaderEnum).path);string tempPath System.IO.Path.GetDirectoryName((down as DownLoaderEnum).path) \temp;System.IO.Directory.CreateDirectory(tempPath); //创建临时文件目录string tempFile tempPath \ System.IO.Path.GetFileName((down as DownLoaderEnum).path) .temp; //临时文件if (System.IO.File.Exists(tempFile)){System.IO.File.Delete(tempFile); //存在则删除}try{FileStream fs new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);// 设置参数HttpWebRequest request WebRequest.Create((down as DownLoaderEnum).url) as HttpWebRequest;//发送请求并获取相应回应数据HttpWebResponse response request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才开始向目标网页发送Post请求Stream responseStream response.GetResponseStream();//创建本地文件写入流//Stream stream new FileStream(tempFile, FileMode.Create);byte[] bArr new byte[1024];int size responseStream.Read(bArr, 0, (int)bArr.Length);while (size 0){//stream.Write(bArr, 0, size);fs.Write(bArr, 0, size);size responseStream.Read(bArr, 0, (int)bArr.Length);}//stream.Close();fs.Close();responseStream.Close();string suffixName (down as DownLoaderEnum).url;int su suffixName.LastIndexOf(/);suffixName (down as DownLoaderEnum).pathsuffixName.Substring(su);// Debug.LogError(suffixName);System.IO.File.Move(tempFile, suffixName);// return true;Debug.LogError(下载完成);}catch (Exception ex){Debug.LogError(错误 ex.Message);//return false;}}
}这是测试代码 using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;public class NewBehaviourScript : MonoBehaviour {public string url http://dl172.80s.im:920/1802/[比得兔]剧场预告片/[比得兔]剧场预告片_hd.mp4;// Use this for initializationstring path ;HttpDownload download new HttpDownload();DownLoaderEnum down;private void Awake(){path Application.streamingAssetsPath;//DirectoryInfo directory new DirectoryInfo(path);//if (directory.Exists)// directory.Create();//Debug.LogError(path);}// Use this for initializationvoid Start(){ThreadPool.SetMaxThreads(5, 5);down new DownLoaderEnum(url, path);//请求url存放地址存放文件名}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space)){ThreadPool.QueueUserWorkItem(download.HttpDownloader, down);}}
}可以看到已经下载完成 转载于:https://www.cnblogs.com/lzy575566/p/8458867.html