当前位置:网站首页>Unity C# 网络学习(六)——FTP(二)
Unity C# 网络学习(六)——FTP(二)
2022-06-24 21:17:00 【帅_shuai_】
Unity C# 网络学习(六)——FTP(二)
一.FTP下载
public class Lesson14 : MonoBehaviour
{
private void Start()
{
NetworkCredential networkCredential = new NetworkCredential("zzs", "zzzsss123");
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri("ftp://127.0.0.1/1.jpg")) as FtpWebRequest;
if (ftpWebRequest == null)
return;
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
Stream stream = ftpWebResponse?.GetResponseStream();
if (stream == null)
return;
string path = Application.persistentDataPath + "/copy1.jpg";
using (FileStream fs = new FileStream(path,FileMode.Create))
{
byte[] buffer = new byte[1024];
int len = stream.Read(buffer,0,buffer.Length);
while (len > 0)
{
fs.Write(buffer,0,len);
len = stream.Read(buffer,0,buffer.Length);
}
}
stream.Close();
Debug.Log("下载完成!");
}
}
二.FTP下载封装
1.协程实现
public void UpLoadFileMono(string fileName, string path, Action action = null)
{
StartCoroutine(UpLoadFile(fileName, path, action));
}
private IEnumerator UpLoadFile(string fileName, string path, Action action = null)
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL) + fileName) as FtpWebRequest;
if (ftpWebRequest == null)
yield break;
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
Stream ftpRequestStream = ftpWebRequest.GetRequestStream();
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] buffer = new byte[10240];
int length = fs.Read(buffer, 0, buffer.Length);
while (length > 0)
{
ftpRequestStream.Write(buffer, 0, length);
length = fs.Read(buffer, 0, buffer.Length);
yield return null;
}
}
ftpWebResponse.Close();
ftpRequestStream.Close();
action?.Invoke();
}
2.Task多线程实现
public async void DownLoadAsync(string downLoadFileName, string outPath, Action action = null)
{
await Task.Run(() =>
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL + downLoadFileName)) as FtpWebRequest;
if (ftpWebRequest == null)
return;
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
Stream stream = ftpWebResponse?.GetResponseStream();
if (stream == null)
return;
using (FileStream fs = new FileStream(outPath, FileMode.Create))
{
byte[] buffer = new byte[10240];
int len = stream.Read(buffer, 0, buffer.Length);
while (len > 0)
{
fs.Write(buffer, 0, len);
len = stream.Read(buffer, 0, buffer.Length);
}
}
ftpWebResponse.Close();
stream.Close();
action?.Invoke();
});
}
三.FTP的其它操作
1.删除文件
public async void DeleteFile(string deleteFileName,Action<bool> action = null)
{
await Task.Run(() =>
{
try
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL + deleteFileName)) as FtpWebRequest;
if (ftpWebRequest == null)
{
action?.Invoke(false);
return;
}
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
if (ftpWebResponse == null)
{
action?.Invoke(false);
return;
}
action?.Invoke(true);
ftpWebResponse.Close();
}
catch (Exception e)
{
action?.Invoke(false);
Debug.Log("删除文件失败:"+e);
}
});
}
2.获取文件大小
public async void GetFileSize(string fileName, Action<long> action = null)
{
await Task.Run(() =>
{
try
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL + fileName)) as FtpWebRequest;
if (ftpWebRequest == null)
{
action?.Invoke(0);
return;
}
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
if (ftpWebResponse == null)
{
action?.Invoke(0);
return;
}
action?.Invoke(ftpWebResponse.ContentLength);
ftpWebResponse.Close();
}
catch (Exception e)
{
action?.Invoke(0);
Debug.Log("获取文件大小失败:"+e);
}
});
}
3.创建文件夹
public async void CreateDir(string dirName, Action<bool> action = null)
{
await Task.Run(() =>
{
try
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL + dirName)) as FtpWebRequest;
if (ftpWebRequest == null)
{
action?.Invoke(false);
return;
}
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
if (ftpWebResponse == null)
{
action?.Invoke(false);
return;
}
action?.Invoke(true);
ftpWebResponse.Close();
}
catch (Exception e)
{
action?.Invoke(false);
Debug.Log("创建文件夹失败:"+e);
}
});
}
4.获取文件列表
public async void GetDirList(string dirName, Action<List<string>> action = null)
{
//记得文件夹名称后面要加'/'
//记得文件夹名称后面要加'/'
//记得文件夹名称后面要加'/'
await Task.Run(() =>
{
try
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
string p = FtpURL + dirName +"/";
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(p)) as FtpWebRequest;
if (ftpWebRequest == null)
{
action?.Invoke(null);
return;
}
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.Proxy = null;
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
FtpWebResponse ftpWebResponse = ftpWebRequest.GetResponse() as FtpWebResponse;
if (ftpWebResponse == null)
{
action?.Invoke(null);
return;
}
Stream s = ftpWebResponse.GetResponseStream();
if (s == null)
{
action?.Invoke(null);
return;
}
List<string> res = new List<string>();
using (StreamReader sr = new StreamReader(s))
{
string str = sr.ReadLine();
while (str != null)
{
res.Add(str);
str = sr.ReadLine();
}
}
s.Close();
action?.Invoke(res);
ftpWebResponse.Close();
}
catch (Exception e)
{
action?.Invoke(null);
Debug.Log("获取文件列表失败:"+e);
}
});
}
其余操作同理
边栏推荐
- sql 聚合函数对 null 的处理[通俗易懂]
- Bi-sql select into
- Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
- The latest QQ wechat domain name anti red PHP program source code + forced jump to open
- 利用 Redis 的 sorted set 做每周热评的功能
- Expectation and variance
- How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
- 弹性蛋白酶中英文说明书
- Bi-sql Union
猜你喜欢

Use redis' sorted set to make weekly hot Reviews

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?

Reading notes at night -- deep into virtual function

Library management system code source code (php+css+js+mysql) complete code source code

Introduction to bi-sql wildcards

AUTOCAD——两种延伸方式

JVM指令

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

天书夜读笔记——内存分页机制
随机推荐
Bi-sql like
C语言边界计算和不对称边界
PMP考试“临门一脚”如何踢得漂亮?
腾讯云WeCity解决方案
Matlab rounding
动手学数据分析 数据建模和模型评估
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
Bi-sql create
卷积与转置卷积
归并排序模板 & 理解
Convolution and transpose convolution
Go language operators (under Lesson 8)
粉丝福利,JVM 手册(包含 PDF)
Bi-sql Union
弹性蛋白酶中英文说明书
木瓜蛋白酶中英文说明书
音频PCM数据计算声音分贝值,实现简单VAD功能
What to learn in VB [easy to understand]
Tencent cloud wecity Hello 2022!