当前位置:网站首页>Unity C # e-learning (VI) -- FTP (I)
Unity C # e-learning (VI) -- FTP (I)
2022-06-25 01:37:00 【Handsome_ shuai_】
Unity C# E-learning ( 6、 ... and )——FTP( One )
One .FTP The key class
1.NetworkCredential class
// be used for FTP Set account number and secret communication voucher during transmission
NetworkCredential networkCredential = new NetworkCredential("zzs", "zzzsss123");
2.FtpWebRequest class
// Create a new WebRequest be used for ftp signal communication
FtpWebRequest ftpWebReq = (FtpWebRequest) WebRequest.Create(new Uri("ftp://127.0.0.1/1.jpg"));
// If it's going on ftp File transfer , Use Abort You can stop
ftpWebReq.Abort();
// Get the stream for uploading GetRequestStream
Stream s = ftpWebReq.GetRequestStream();
// obtain ftp Server response
FtpWebResponse ftpWebRep = ftpWebReq.GetResponse() as FtpWebResponse;
//======= Key members =======
// Set communication credentials
ftpWebReq.Credentials = networkCredential;
// Whether to close when the request is completed ftp Control connection of the server ( The default is true, Don't shut down )
ftpWebReq.KeepAlive = false;
// Operation command settings (WebRequestMethods.Ftp There is a corresponding static variable in the static class to set the value )
ftpWebReq.Method = WebRequestMethods.Ftp.DownloadFile;
// Whether to use binary to transmit
ftpWebReq.UseBinary = true;
// Rename file
ftpWebReq.RenameTo = "111.jpg";
3.FtpWebResponse class
FtpWebResponse response = ftpWebReq.GetResponse() as FtpWebResponse;
// close ftpResponse
if (response == null)
return;
response.Close();
// Returns the stream returned from the server
Stream sm = response.GetResponseStream();
//===== Key members =====
//1. Get the length of the received data
Debug.Log(response.ContentLength);
//2. The type of data received
Debug.Log(response.ContentType);
//3. The latest status code issued by the server
Debug.Log(response.StatusCode);
//4. The text of the latest status code issued by the server
Debug.Log(response.StatusDescription);
//5. When establishing a connection before logging in FTP The message sent by the server
Debug.Log(response.BannerMessage);
//6.FTP Message sent by the server at the end of the session
Debug.Log(response.ExitMessage);
//7.FTP The date and time when the file on the server was uploaded and modified
Debug.Log(response.LastModified);
Two .FTP Upload
public class Lesson12 : MonoBehaviour
{
private void Start()
{
try
{
NetworkCredential networkCredential = new NetworkCredential("zzs", "zzzsss123");
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri("ftp://127.0.0.1/pic.jpg")) as FtpWebRequest;
if (ftpWebRequest == null)
return;
// Must be set to null , Otherwise an error
ftpWebRequest.Proxy = null;
ftpWebRequest.Credentials = networkCredential;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
Stream upLoadStream = ftpWebRequest.GetRequestStream();
string path = Application.streamingAssetsPath + "/test.jpg";
using (FileStream fs = new FileStream(path, FileMode.Open))
{
byte[] buffer = new byte[1024];
int readLength = fs.Read(buffer, 0, buffer.Length);
while (readLength > 0)
{
upLoadStream.Write(buffer,0,readLength);
readLength = fs.Read(buffer, 0, buffer.Length);
}
fs.Close();
}
upLoadStream.Close();
Debug.Log(" complete !");
}
catch (Exception e)
{
Debug.Log(e);
return;
}
}
}
3、 ... and .FTP Upload package
1. Process implementation
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;
}
}
ftpRequestStream.Close();
action?.Invoke();
}
2.Task Multithreaded implementation
public async void UpLoadFileAsync(string fileName, string path, Action action = null)
{
await Task.Run(() =>
{
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL) + fileName) as FtpWebRequest;
if (ftpWebRequest == null)
return;
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);
}
}
ftpRequestStream.Close();
action?.Invoke();
});
}
边栏推荐
- Properties of DOM
- Basic knowledge of assembly language (2) -debug
- Tencent cloud wecity Hello 2022!
- 最长连续序列[扩散法+空间换时间]
- Tencent moved!
- Bi-sql between
- Bi-sql delete
- Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
- Chinese and English instructions of trypsin
- Abnova a4gnt polyclonal antibody
猜你喜欢
随机推荐
15. several methods of thread synchronization
广发期货安全吗?开户需要什么东西?
excel 汉字转拼音「建议收藏」
国内炒股开户正规安全的具体名单
uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法
Numerical scheme simulation of forward stochastic differential equations with Markov Switching
为猪脸识别而进行自己数据集的构建、训练「建议收藏」
百度语音合成语音文件并在网站中展示
全排列II[存在相同元素去重 + 标准回溯]
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
Audio PCM data calculates sound decibel value to realize simple VAD function
Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
Use redis' sorted set to make weekly hot Reviews
Linux64Bit下安装MySQL5.6-不能修改root密码
搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
实验5 8254定时/计数器应用实验【微机原理】【实验】
After the college entrance examination, the following four situations will inevitably occur:
CCNP的BGP部分笔记
Abnova丨5-甲基胞嘧啶多克隆抗体中英文说明





![全排列II[存在相同元素去重 + 标准回溯]](/img/d3/93ddb49e580be60be4f056f141b782.png)


