前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity开发--网络(二)服务器使用get请求下载服务器资源

Unity开发--网络(二)服务器使用get请求下载服务器资源

作者头像
孙寅
发布2020-06-02 17:12:23
1.1K0
发布2020-06-02 17:12:23
举报
文章被收录于专栏:宜达数字
从服务器下载文件到本地

第一种:http get请求

代码语言:javascript
复制
    string urlPath = "http://www........";   //写个网络资源路径
    string localPath = @"D:VR04\longtu.mp4";

       /// <summary>
      /// 下载文件
      /// </summary>

    IEnumerator DownLoadFile(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        HttpWebResponse hw = (HttpWebResponse)request.GetResponse();
        Stream stream = hw.GetResponseStream();
        FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.Write);
        long length = hw.ContentLength;
        long currentNum = 0;
        decimal currentProgress = 0;
 
        while (currentNum < length)
        {
            byte[] buffer = new byte[1024];
            currentNum += stream.Read(buffer, 0, buffer.Length);
            fileStream.Write(buffer, 0, buffer.Length);
            if (currentNum % 1024 == 0)
            {
                currentProgress = Math.Round(Convert.ToDecimal(Convert.ToDouble(currentNum) /Convert.ToDouble(length) * 100), 4);
                Debug.Log("当前下载文件大小:" + length.ToString() + "字节   当前下载大小:" + currentNum + "字节下载进度" + currentProgress.ToString() + "%");
            }
            else
            {
                Debug.Log("当前下载文件大小:" + length.ToString() + "字节   当前下载大小:" + currentNum + "字节"+ "字节 下载进度" + 100 + "%");

            }
            currentnn = currentProgress;
            yield return false;
        }
        hw.Close();
        stream.Close();
        fileStream.Close();
    }
    decimal currentNumShow;
    GUIStyle guistyle = new GUIStyle();

    void OnGUI()
    {
        guistyle.fontSize = 80;
        GUI.Label(new Rect(50, 50, 50, 50), currentNumShow.ToString(), guistyle);
    }

第二种: www

代码语言:javascript
复制
    string urlPath = "http://www.....";//资源网络路径
    string file_SaveUrl = @"D:\test.rar";//资源保路径
    FileInfo file; 
    void Start ()
    {
        file = new FileInfo(file_SaveUrl);
        Debug.Log(file_SaveUrl);
        StartCoroutine(DownFile(urlPath));
    }

   
    /// <summary>
    /// 下载文件
    /// </summary>
    IEnumerator DownFile(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.isDone)
        {
            Debug.Log("下载完成");
            byte[] bytes = www.bytes;
            CreatFile(bytes);
        }
    }

    /// <summary>
    /// 创建文件
    /// </summary>
    /// <param name="bytes"></param>
    void CreatFile(byte[] bytes)
    {
        Stream stream;
        stream = file.Create();
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
        stream.Dispose();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 从服务器下载文件到本地
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档