在unity 中,使用http请求,下载文件到可读可写路径

在这里我用了一个线程池,线程池参数接收一个带有object参数的,无返回值的委托 ,下载用到的核心代码,网上拷贝的,他的核心就是发起一个web请求,然后得到请求的响应,读取响应的流

在unity 中,使用http请求,下载文件到可读可写路径

剩下的都是常见的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>下载文件地址</param> /// <param>文件存放地址,包含文件名</param> /// <returns></returns> public 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).path+suffixName.Substring(su); // Debug.LogError(suffixName); System.IO.File.Move(tempFile, suffixName); // return true; Debug.LogError("下载完成"); } catch (Exception ex) { Debug.LogError("错误==>>" + ex.Message); //return false; } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwyzpx.html