当前位置:网站首页>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();
});
}
边栏推荐
- Pbcms adding cyclic digital labels
- IPC机制
- 15. several methods of thread synchronization
- 1. 封装自己的脚手架 2.创建代码模块
- Unity C# 网络学习(六)——FTP(一)
- Tianshu night reading notes -- memory paging mechanism
- 天书夜读笔记——内存分页机制
- Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
- Bi-sql - different join
- Poj3669 meteor shower (BFS pretreatment)
猜你喜欢
Bi-sql Union
"One good programmer is worth five ordinary programmers!"
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Bi-sql delete
修身励学篇
Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations
Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代
Bi SQL alias
JVM directive
谷歌浏览器控制台 f12怎么设置成中文/英文 切换方法,一定要看到最后!!!
随机推荐
AutoCAD - two extension modes
Listen to the markdown file and hot update next JS page
php中preg_replace如何替换变量数据
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
Bi SQL drop & alter
Tencent cloud wecity solution
搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
梦想CAD云图与GIS结合演示
ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
最长连续序列[扩散法+空间换时间]
Unity C# 网络学习(六)——FTP(一)
How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
WinXP内核驱动调试
带马尔科夫切换的正向随机微分方程数值格式模拟
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
Abnova 5-methylcytosine polyclonal antibody
Boutique enterprise class powerbi application pipeline deployment
JVM指令
天书夜读笔记——反汇编引擎xde32
Abnova丨BSG 单克隆抗体中英文说明