日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在unity 中,使用http请求,下载文件到可读可写路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在這里我用了一個線程池,線程池參數接收一個帶有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 name="url">下載文件地址</param>/// <param name="path">文件存放地址,包含文件名</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;}} }

  

  這是測試代碼

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

總結

以上是生活随笔為你收集整理的在unity 中,使用http请求,下载文件到可读可写路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。