当前位置:网站首页>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
边栏推荐
猜你喜欢

Bi-sql create

TC对象结构和简称

PMP考试“临门一脚”如何踢得漂亮?
![uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法](/img/8b/0e982711c225ec8b0a2b90819d8a11.png)
uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法

Pbcms adding cyclic digital labels

数组中关于sizeof()和strlen

JVM directive

15. several methods of thread synchronization

Deep learning LSTM model for stock analysis and prediction

How to store dataframe data in pandas into MySQL
随机推荐
Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
归并排序求逆序数
监听 Markdown 文件并热更新 Next.js 页面
Audio PCM data calculates sound decibel value to realize simple VAD function
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
excel 汉字转拼音「建议收藏」
"One good programmer is worth five ordinary programmers!"
lnmp环境安装ffmpeg,并在Yii2中使用
Boutique enterprise class powerbi application pipeline deployment
Excel Chinese character to pinyin "suggestions collection"
Cobalt strike installation tutorial
How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
Expectation and variance
An Chaoyun: "one cloud with multiple cores" supports the implementation of the national information innovation government cloud
中金证券靠谱吗?开证券账户安全吗?
Google browser console F12 how to set the Chinese / English switching method, we must see the last!!!
Basic knowledge of assembly language (2) -debug
Matlab rounding
leetcode:2104. 子数组范围和