[通过Android上的FTP下载文件

问题描述 投票:1回答:2

我需要在计算机(服务器)和我的Android设备(客户端)之间建立与本地FTP协议的连接。这应下载要在Android Unity应用场景中使用的文件(图像,OBJ等)。我使用WWW类创建此连接,并且在作为客户端在另一台计算机上运行的Unity Player中可以正常工作。一旦我导出了与Android apk相同的场景,它就无法正常工作(我确定FTP连接是稳定的并且可以正常工作,因为我能够从浏览器访问文件)。有人知道我的代码在Android Unity应用程序上使用FTP协议是否有其他方法或有问题吗? (客户端不需要任何授权,身份验证是匿名的)这是我用来下载场景内的一幅图像并将其渲染为精灵的代码。

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Net;
 using System.IO;

 public class ClientFTP : MonoBehaviour
 {
   public UnityEngine.UI.Image label;

   IEnumerator Start ()
   {
       // Create the connection and whait until it is established
       string url = ("ftp://192.168.10.11/prova.png");
       WWW ftpconnection = new WWW (url);
       yield return ftpconnection;
       // Download the image and render it as a texture
       Texture2D tex = new Texture2D (250, 192);
       ftpconnection.LoadImageIntoTexture (tex);
       // Assign the texture to a new sprite
       Sprite s = Sprite.Create (tex, new Rect (0, 0, 250f, 192f), new Vector2 (0.5f, 0.5f), 300);
       label.preserveAspect = true;
       label.sprite = s;

    }
 }
android unity3d web ftp client-server
2个回答
1
投票

如果不需要凭据来访问文件,为什么要使用FTP?您可以只将文件放在服务器中,然后使用WWWUnityWebRequest API访问它们。

要回答您的FTP问题,UnityWebRequest不能与FTP协议一起使用。这就是WWW API的用途。

下面是FtpWebRequest的样本。

FtpWebRequest

用法

下载并保存(无凭据)

private byte[] downloadWithFTP(string ftpUrl, string savePath = "", string userName = "", string password = "")
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpUrl));
    //request.Proxy = null;

    request.UsePassive = true;
    request.UseBinary = true;
    request.KeepAlive = true;

    //If username or password is NOT null then use Credential
    if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
    {
        request.Credentials = new NetworkCredential(userName, password);
    }

    request.Method = WebRequestMethods.Ftp.DownloadFile;

    //If savePath is NOT null, we want to save the file to path
    //If path is null, we just want to return the file as array
    if (!string.IsNullOrEmpty(savePath))
    {
        downloadAndSave(request.GetResponse(), savePath);
        return null;
    }
    else
    {
        return downloadAsbyteArray(request.GetResponse());
    }
}

byte[] downloadAsbyteArray(WebResponse request)
{
    using (Stream input = request.GetResponseStream())
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while (input.CanRead && (read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
}

void downloadAndSave(WebResponse request, string savePath)
{
    Stream reader = request.GetResponseStream();

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(savePath)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(savePath));
    }

    FileStream fileStream = new FileStream(savePath, FileMode.Create);


    int bytesRead = 0;
    byte[] buffer = new byte[2048];

    while (true)
    {
        bytesRead = reader.Read(buffer, 0, buffer.Length);

        if (bytesRead == 0)
            break;

        fileStream.Write(buffer, 0, bytesRead);
    }
    fileStream.Close();
}

下载并保存(带有凭据)

string path = Path.Combine(Application.persistentDataPath, "FTP Files");
path = Path.Combine(path, "data.png");
downloadWithFTP("ftp://yourUrl.com/yourFile", path);

仅下载(无凭据)

string path = Path.Combine(Application.persistentDataPath, "FTP Files");
path = Path.Combine(path, "data.png");
downloadWithFTP("ftp://yourUrl.com/yourFile", path, "UserName", "Password");

仅下载(带有证书)

string path = Path.Combine(Application.persistentDataPath, "FTP Files");
path = Path.Combine(path, "data.png");
byte[] yourImage = downloadWithFTP("ftp://yourUrl.com/yourFile", "");

//Convert to Sprite
Texture2D tex = new Texture2D(250, 192);
tex.LoadImage(yourImage);
Sprite s = Sprite.Create(tex, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));

0
投票

公共布尔ftpDownloadVideo(){

String desFilePath =(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS))。toString(); /// xet trong moi truong SD卡////字符串srcFilePath =“ /detect_disease/rice_detect_disease_2019_09_24__10_07_08.avi”;布尔状态=假;Log.d(TAG,“ 2”);

string path = Path.Combine(Application.persistentDataPath, "FTP Files");
path = Path.Combine(path, "data.png");
byte[] yourImage = downloadWithFTP("ftp://yourUrl.com/yourFile", "", "UserName", "Password");

//Convert to Sprite
Texture2D tex = new Texture2D(250, 192);
tex.LoadImage(yourImage);
Sprite s = Sprite.Create(tex, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
© www.soinside.com 2019 - 2024. All rights reserved.