UH3L4——上传AB包和信息文件
搭建FTP服务器
本次我们使用别人做好的FTP服务器软件来搭建FTP服务器:更多内容请看:UN3L2——搭建FTP服务器
注意:实际开发中,搭建FTP服务器的工作一般由后端程序来完成,不需要我们前端程序来完成
后端程序搭建好后,给你相关的信息,我们访问即可
下载安装Serv-U等FTP服务器软件,在想要作为FTP服务器的电脑上运行即可
(推荐使用同一局域网(路由器或者WIFI)下的第二台设备学习,前提是有)
- 打开Serv-U,创建域,直接不停下一步即可,加密选择使用单向加密
 
- 创建用于上传的FTP账号密码
创建用于下载的FTP匿名账号(用户名:Anonymous,密码:无,设置只读权限) 
- 检查作为服务器设备当前IP地址,在cmd或者powershell里输入ipconfig即可查看,客户端使用该IP地址访问服务器
 
上传AB包和资源对比文件
前置知识点
- C#异步函数
 
- Task任务类
 
- UN3L4——FTP上传文件
 
- 上节课生成的AB包资源对比文件
 
实现流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
   | using System; using System.IO; using System.Net; using System.Threading.Tasks; using UnityEditor; using UnityEngine;
  public class UploadAB {     [MenuItem("AB包工具/上传AB包和对比文件")]     private static void UploadABFile()     {         DirectoryInfo directory = new DirectoryInfo(Application.dataPath + "/ArtRes/AB/PC/");         FileInfo[] fileInfos = directory.GetFiles();
          foreach (FileInfo info in fileInfos)         {                          if (info.Extension == "" ||                 info.Extension == ".txt")             {                                  FtpUploadFile(info.FullName, info.Name);             }         }     }
      private static async void FtpUploadFile(string filePath, string fileName)     {         await Task.Run(() =>         {             try             {                                  FtpWebRequest request = FtpWebRequest.Create(new Uri("ftp://192.168.1.101/AB/PC/" + fileName)) as FtpWebRequest;                                  NetworkCredential credential = new NetworkCredential("MrTang", "MrTang123");                 request.Credentials = credential;                                  request.Proxy = null;                 request.KeepAlive = false;                 request.Method = WebRequestMethods.Ftp.UploadFile;                 request.UseBinary = true;                                  Stream upLoadStream = request.GetRequestStream();                                  using (FileStream file = File.OpenRead(filePath))                 {                                          byte[] bytes = new byte[2048];                     int contentLength = file.Read(bytes, 0, bytes.Length);                     while (contentLength > 0)                     {                         upLoadStream.Write(bytes, 0, contentLength);                         contentLength = file.Read(bytes, 0, bytes.Length);                     }                     file.Close();                     upLoadStream.Close();                 }                 Debug.Log($"{fileName}上传成功");             }             catch (Exception e)             {                 Debug.Log($"{fileName}上传失败:{e.Message}");             }         });     } }
   |