2017-04-14 8 views
1

컴퓨터 (서버)와 안드로이드 장치 (클라이언트)간에 로컬 FTP 프로토콜로 연결해야합니다. Android Unity 앱 화면에서 사용할 파일 (이미지, OBJ 등)을 다운로드해야합니다. 나는이 연결을 생성하기 위해 WWW 클래스를 사용했고 클라이언트로서 다른 컴퓨터에서 실행되는 Unity 플레이어에서 잘 작동합니다. Android APK와 동일한 장면을 내 보내면 작동하지 않습니다 (FTP 연결이 안정적이며 브라우저의 파일에 액세스 할 수 있기 때문에 작동 함). 누군가 다른 방법이 있는지 또는 내 Android 코드에서 FTP 프로토콜을 사용하는 데 문제가 있는지 알고 있습니까? (클라이언트는 인증을 필요로하지 않으며 인증은 익명입니다.) 다음은 장면 내에서 하나의 이미지를 다운로드하여 스프라이트로 렌더링하는 데 사용하는 코드입니다.안드로이드에서 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; 

    } 
} 
+0

예, 감사합니다! 이제 효과가있다. – Iacopooo

답변

1

파일에 액세스하기 위해 자격 증명이 필요없는 경우 FTP를 사용해야하는 이유는 무엇입니까? 파일을 서버에 배치 한 다음 WWW 또는 UnityWebRequest API를 사용하여 액세스 할 수 있습니다.

FTP 질문에 대답하려면 FTP 프로토콜과 함께 WWW을 사용하지 않아야합니다. 이것이 FtpWebRequest API가 사용 된 것입니다.

다음은 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)); 

다운로드 :

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)); 
관련 문제