当前位置:网站首页>Unity C# 网络学习(六)——FTP(二)
Unity C# 网络学习(六)——FTP(二)
2022-06-24 21:17:00 【帅_shuai_】
Unity C# 网络学习(六)——FTP(二)
一.FTP下载
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("下载完成!");
}
}
二.FTP下载封装
1.协程实现
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多线程实现
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();
});
}
三.FTP的其它操作
1.删除文件
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("删除文件失败:"+e);
}
});
}
2.获取文件大小
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("获取文件大小失败:"+e);
}
});
}
3.创建文件夹
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("创建文件夹失败:"+e);
}
});
}
4.获取文件列表
public async void GetDirList(string dirName, Action<List<string>> action = null)
{
//记得文件夹名称后面要加'/'
//记得文件夹名称后面要加'/'
//记得文件夹名称后面要加'/'
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("获取文件列表失败:"+e);
}
});
}
其余操作同理
边栏推荐
- 期望与方差
- Bi-sql top
- Bi SQL constraints
- Tencent cloud wecity solution
- Bi-sql between
- CCNP的BGP部分笔记
- JVM directive
- 音频PCM数据计算声音分贝值,实现简单VAD功能
- After the college entrance examination, the following four situations will inevitably occur:
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
猜你喜欢

Powerbi - for you who are learning

Bi-sql - join

Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

第04天-文件IO

数组中关于sizeof()和strlen

Bi-sql index

Using macro code to generate handwriting automatically in word or WPS

利用 Redis 的 sorted set 做每周热评的功能
随机推荐
IPC机制
Install mysql5.6 under linux64bit - the root password cannot be modified
Program.launch(xxx)打开文件
搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
PMP考试“临门一脚”如何踢得漂亮?
实验5 8254定时/计数器应用实验【微机原理】【实验】
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
mysql查询时间戳转换成日期格式
腾讯完成全面上云 打造国内最大云原生实践
Abnova丨CSV 磁珠中英文说明
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
void* 指针
Bi skill - judge 0 and null
Redis persistence
百度语音合成语音文件并在网站中展示
[practical series] full WiFi coverage at home
Introduction to bi-sql wildcards
为猪脸识别而进行自己数据集的构建、训练「建议收藏」