当前位置:网站首页>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();
});
}
边栏推荐
- Chinese and English instructions of trypsin
- 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
- Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代
- void* 指针
- Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
- leetcode:2104. 子数组范围和
- String common methods
- 明日考试 最后一天如何备考?二造考点攻略全整理
- Tencent cloud wecity Hello 2022!
- 粉丝福利,JVM 手册(包含 PDF)
猜你喜欢

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

Bi-sql - join

同一服务器两个端口不同的应用session覆盖解决方案

Bi skill - judge 0 and null

leetcode:2104. Subarray range and

Linux64Bit下安装MySQL5.6-不能修改root密码

Abnova a4gnt polyclonal antibody

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

Abnova 5-methylcytosine polyclonal antibody
![最长连续序列[扩散法+空间换时间]](/img/db/7b0d1b0db7015e887340723505153a.png)
最长连续序列[扩散法+空间换时间]
随机推荐
MySQL gets the primary key and table structure of the table
最长连续序列[扩散法+空间换时间]
leetcode:2104. 子数组范围和
Chinese and English instructions of trypsin
What to learn in VB [easy to understand]
This national day! Tencent cloud wecity will accompany you to travel and light up the city landmark
Bi-sql between
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
After the college entrance examination, the following four situations will inevitably occur:
C language boundary calculation and asymmetric boundary
监听 Markdown 文件并热更新 Next.js 页面
弹性蛋白酶中英文说明书
AssertionError: CUDA unavailable, invalid device 0 requested
JVM directive
Basic knowledge of assembly language (2) -debug
动手学数据分析 数据建模和模型评估
How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
lnmp环境安装ffmpeg,并在Yii2中使用
The innovation consortium of Haihe laboratory established gbase and became one of the first member units of the innovation Consortium (Xinchuang)
全排列II[存在相同元素去重 + 标准回溯]