当前位置:网站首页>Unity C # e-learning (VI) -- FTP (II)
Unity C # e-learning (VI) -- FTP (II)
2022-06-25 01:37:00 【Handsome_ shuai_】
Unity C# E-learning ( 6、 ... and )——FTP( Two )
One .FTP download
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(" Download complete !");
}
}
Two .FTP Download package
1. Process implementation
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 Multithreaded implementation
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();
});
}
3、 ... and .FTP Other operations of
1. Delete file
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(" Delete file failed :"+e);
}
});
}
2. Get file size
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(" Failed to get file size :"+e);
}
});
}
3. Create folder
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(" Failed to create folder :"+e);
}
});
}
4. Get file list
public async void GetDirList(string dirName, Action<List<string>> action = null)
{
// Remember to add... After the folder name '/'
// Remember to add... After the folder name '/'
// Remember to add... After the folder name '/'
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(" Failed to get file list :"+e);
}
});
}
Other operations are the same
边栏推荐
猜你喜欢

AutoCAD - two extension modes

Ps5 connected to oppo K9 TV does not support 2160p/4k

Abnova a4gnt polyclonal antibody

Huawei laptop, which grew against the trend in Q1, is leading PC into the era of "smart office"

Bi SQL drop & alter

Bi-sql top

How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site

Cobalt strike installation tutorial

void* 指针

Reading notes at night -- deep into virtual function
随机推荐
纹理增强
Poj3669 meteor shower (BFS pretreatment)
How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
为猪脸识别而进行自己数据集的构建、训练「建议收藏」
AssertionError: CUDA unavailable, invalid device 0 requested
广发期货安全吗?开户需要什么东西?
Listen to the markdown file and hot update next JS page
JVM directive
TC对象结构和简称
第04天-文件IO
Cloud development technology summit · public welfare programming challenge [hot registration]!
明日考试 最后一天如何备考?二造考点攻略全整理
归并排序模板 & 理解
sql 聚合函数对 null 的处理[通俗易懂]
What to learn in VB [easy to understand]
MySQL gets the primary key and table structure of the table
AutoCAD - two extension modes
php easywechat 和 小程序 实现 长久订阅消息推送
Bi-sql like
ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型