首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在后台下载FTPES密码保护文件

如何在后台下载FTPES密码保护文件
EN

Stack Overflow用户
提问于 2018-08-23 12:40:06
回答 1查看 185关注 0票数 0

在过去的几个星期里,我一直在乱搞很多事情,似乎找不到正确的方法。我是在统一中这样做的,当然,本质应该在一个环境中维护。

我在FTPES上有3个音频文件和一个文本文档,这将在FTP上明确显示TLS:

我已经使用以下代码成功登录,以验证该文件的存在:

代码语言:javascript
复制
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]

尽管如此,我也成功地下载了文件的许多方式..我遇到的问题如下:

  • 使用FtpWebRequest,我可以下载文件,并能够以AudioClip的形式加载文件(老实说,我不知道为什么它会随机停止工作,下载时使用stopped请求,即.)使用WWW类,但因为我似乎找不到办法把它推入后台。我试着用streamWriter编写文件,但它似乎不起作用。
  • 使用UnityWebRequest,我不能登录到FTPS服务器,因为它是为基于HTTP的通信设计的。
  • 使用WebClient,我能够登录到服务器(如上面所示注册前缀并将其与useSsl bool设置为true的低级别相关联)
代码语言:javascript
复制
- 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:

代码语言:javascript
复制
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()中的服务器日志;

代码语言:javascript
复制
(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的示例所示:

代码语言:javascript
复制
(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.
EN

回答 1

Stack Overflow用户

发布于 2018-08-23 20:41:30

我剥去了我在Unity中工作的一个旧FTP下载程序脚本。您可能希望保存流资产文件夹中的文件。不过这会对你有帮助的。我用这个下载视频--他们从StreamingAssets文件夹中播放它们,但是下载器确实起了作用。

代码语言:javascript
复制
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());
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51986061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档