当前位置:网站首页>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
边栏推荐
- 屡获大奖的界面控件开发包DevExpress v22.1官宣发布
- Introduction to bi-sql wildcards
- Fan benefits, JVM manual (including PDF)
- Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
- Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
- Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations
- Redis persistence
- Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]
- Reverse ordinal number by merge sort
- Abnova CSV magnetic beads description in Chinese and English
猜你喜欢

Ideas and examples of divide and conquer

PMP考试“临门一脚”如何踢得漂亮?
![Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]](/img/e2/7da59a566e4ccb8e43f2a64c0420e7.png)
Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]

PS5连接OPPO K9电视不支持2160P/4K

实验5 8254定时/计数器应用实验【微机原理】【实验】

1. 封装自己的脚手架 2.创建代码模块

Cobalt strike installation tutorial

15. several methods of thread synchronization

百度语音合成语音文件并在网站中展示

Bi skill - judge 0 and null
随机推荐
C语言边界计算和不对称边界
第04天-文件IO
Deep learning LSTM model for stock analysis and prediction
Expectation and variance
Audio PCM data calculates sound decibel value to realize simple VAD function
安超云:“一云多芯”支持国家信创政务云落地
Bi-sql - different join
纹理增强
Assembly language (4) function transfer parameters
Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
粉丝福利,JVM 手册(包含 PDF)
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
void* 指针
论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
PHP easywechat and applet realize long-term subscription message push
归并排序模板 & 理解
JVM directive
Combined with practice, you will understand redis persistence
Pbcms adding cyclic digital labels