在过去的几个星期里,我一直在乱搞很多事情,似乎找不到正确的方法。我是在统一中这样做的,当然,本质应该在一个环境中维护。
我在FTPES上有3个音频文件和一个文本文档,这将在FTP上明确显示TLS:
我已经使用以下代码成功登录,以验证该文件的存在:
WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
ftpRequest = (FtpWebRequest)WebRequest.Create("ftps://" + IP + "/" + fileEndPath);
ftpRequest.Credentials = new NetworkCredential(User, Password);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
ServicePointManager.Expect100Continue = true;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ValidConnection = true;[/code]尽管如此,我也成功地下载了文件的许多方式..我遇到的问题如下:
- Using .DownloadFile not able to download file, hangs Unity Main Thread.
- Using .DownloadFileAsyn able to download the the file, but have no read permission, and Complete event never gets called to continue into loading the file using WWW.
我在这篇文章中使用的是WebClient.DownloadFileAsyn:
internal void Download(FileType fileType)
{
IsDownloading = true;
if (ValidConnection)
{
try
{
StartCoroutine(SetFileCreation());
using (WebClient client = new WebClient())
{
WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
client.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadFileProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
client.DownloadFileAsync(ftpIP, Application.dataPath + "/StreamingAssets/" + ftpFilePath, fileType);
}
}
catch (WebException ex)
{
Debug.LogError(ex.Message + ex.StackTrace + "\n");
}
}
else
IsDownloading = false;
}SetFileCreation();是一个验证位置的方法,如果它存在,则覆盖,否则在继续之前创建目录。
现在我没有任何干净的代码(除了我已经展示的代码)来显示我一直在做的事情,但是如果有人能告诉我这方面的一个很好的例子(我还没有在下载FTPS in Unity中找到很多东西),或者只是分享他们如何从底层处理这个问题,我们会非常感激的。
顺便提一句,我知道我的FTP服务器通过https://ftptest.net/和我的服务器日志(我自己托管它,所以我可以完全控制设置)显示所有正确的消息,直到RETR并传输成功的消息。
我已经看到了很多关于FTPS的文章,包括各种下载方式,不同级别的网络通信,高层次和低级别,以及如何将文件从文件夹中实时加载到Unity中。但没有什么东西能把这一切一刀切,很好,很干净。亲一下!
作为我们中的大多数人,我有更大的目标要关注,不能让这个细节继续拖延开发。
编辑(服务器日志):
以下是我的WebClient.DownloadAsync()中的服务器日志;
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> Connected, sending welcome message...
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> AUTH TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 234 Using authentication type TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> SSL connection established
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PBSZ 0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 PBSZ=0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PROT P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 Protection level set to P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> USER Test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 331 Password required for test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PASS ********
(000657)08/23/2018 22:51:32 - test ([MyIP])> 230 Logged on
(000657)08/23/2018 22:51:32 - test ([MyIP])> OPTS utf8 on
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 UTF8 mode enabled
(000657)08/23/2018 22:51:32 - test ([MyIP])> PWD
(000657)08/23/2018 22:51:32 - test ([MyIP])> 257 "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> CWD /
(000657)08/23/2018 22:51:32 - test ([MyIP])> 250 CWD successful. "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> TYPE I
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 Type set to I
(000657)08/23/2018 22:51:32 - test ([MyIP])> PASV
(000657)08/23/2018 22:51:32 - test ([MyIP])> 227 Entering Passive Mode ([MyIP],195,146)
(000657)08/23/2018 22:51:32 - test ([MyIP])> RETR MainQuests.txt
(000657)08/23/2018 22:51:32 - test ([MyIP])> 150 Connection accepted
(000657)08/23/2018 22:51:32 - test ([MyIP])> SSL connection for data connection established
(000657)08/23/2018 22:51:32 - test ([MyIP])> 226 Transfer OK值得注意的是,连接是维护的。它不会关闭,因为连接设置为在文件下载完成后关闭.
下面是我在使用C#任务时获得的日志,如Dan Flanagan的示例所示:
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> Connected, sending welcome message...
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> AUTH TLS
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 234 Using authentication type TLS
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> SSL connection established
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PBSZ 0
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 200 PBSZ=0
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PROT P
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 200 Protection level set to P
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> USER Test
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> 331 Password required for test
(000659)08/23/2018 22:58:10 - (not logged in) ([MyIP])> PASS ********
(000659)08/23/2018 22:58:10 - test ([MyIP])> 230 Logged on
(000659)08/23/2018 22:58:10 - test ([MyIP])> OPTS utf8 on
(000659)08/23/2018 22:58:10 - test ([MyIP])> 200 UTF8 mode enabled
(000659)08/23/2018 22:58:10 - test ([MyIP])> PWD
(000659)08/23/2018 22:58:10 - test ([MyIP])> 257 "/" is current directory.
(000659)08/23/2018 22:58:10 - test ([MyIP])> CWD /
(000659)08/23/2018 22:58:10 - test ([MyIP])> 250 CWD successful. "/" is current directory.
(000659)08/23/2018 22:58:10 - test ([MyIP])> TYPE I
(000659)08/23/2018 22:58:10 - test ([MyIP])> 200 Type set to I
(000659)08/23/2018 22:58:10 - test ([MyIP])> PASV
(000659)08/23/2018 22:58:10 - test ([MyIP])> 227 Entering Passive Mode ([MyIP],195,147)
(000659)08/23/2018 22:58:10 - test ([MyIP])> RETR MainQuests.txt
(000659)08/23/2018 22:58:10 - test ([MyIP])> 150 Connection accepted
(000659)08/23/2018 22:58:10 - test ([MyIP])> SSL connection for data connection established
(000659)08/23/2018 22:58:10 - test ([MyIP])> 426 Connection closed; transfer aborted. (000659)08/23/2018 22:58:10 - test ([MyIP])> QUIT
(000659)08/23/2018 22:58:10 - test ([MyIP])> 221 Goodbye
(000659)08/23/2018 22:58:10 - test ([MyIP])> disconnected.发布于 2018-08-23 20:41:30
我剥去了我在Unity中工作的一个旧FTP下载程序脚本。您可能希望保存流资产文件夹中的文件。不过这会对你有帮助的。我用这个下载视频--他们从StreamingAssets文件夹中播放它们,但是下载器确实起了作用。
using System;
using System.Threading.Tasks;
using System.IO;
using System.Net;
public class FTPDownloader : MonoBehaviour
{
public bool editorDownload;
public string savePath;
public string saveDir;
public string username;
public string pswd;
public void StartDownload(string serverIP, string serverFolder, string fileName)
{
string url = string.Format("{0}{1}{2}{3}", "ftp://", serverIP, serverFolder, fileName);
Task task = new Task(() =>
{
Download(url, fileName);
});
task.Start();
}
void Download(string url, string fileName)
{
if (!editorDownload)
{
return;
}
string path = Path.Combine(saveDir, fileName);
try
{
if(File.Exists(path))
{
//already downloaded
return;
}
//Debug.Log(url);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential(username, pswd);
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(path))
{
byte[] buffer = new byte[10240];
int read;
int total = 0;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
total += read;
}
}
}
catch (Exception e)
{
if (File.Exists(path))
{
File.Delete(path);
}
//Debug.Log(e.ToString());
}
}
}https://stackoverflow.com/questions/51986061
复制相似问题