Unity3D - 메모장

quest3dkorea.com


통계 위젯 (화이트)

128
178
140869


ftp upload / download / get file list Unity3D 스크립트

public class UtilFTP : MonoBehaviour {
        public static void call_uploadfile(string uploadfilename, string ftptargetpath, string ftpserver, string username, string password) {
            try {
                string filename = Path.GetFileName(uploadfilename);
                Uri newftpserver = new Uri(ftpserver);
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(newftpserver + ftptargetpath + filename);
                ftp.Credentials = new NetworkCredential(username, password);
                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;

                FileStream fs = File.OpenRead(uploadfilename);
                Debug.Log("uploadfilename : " + uploadfilename);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            } catch (WebException ex) {
                Debug.Log(ex.ToString());
            }

        }

        public static void DownloadFile(string serverFulladd, string localfilepath, string id, string pass) {
            using (WebClient wc = new WebClient()) {
                wc.Credentials = new NetworkCredential(id, pass);
                wc.DownloadFile(serverFulladd, localfilepath);
            }
        }
    
    }

public string[] GetFileList(string ftpadd, string folder) {
        Debug.Log(firsttext.text);
        string[] downloadFiles;
        StringBuilder result = new StringBuilder();
        WebResponse response = null;
        StreamReader reader = null;
        try {
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpadd + folder));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(id, pass);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            reqFTP.Proxy = null;
            reqFTP.KeepAlive = false;
            reqFTP.UsePassive = false;
            response = reqFTP.GetResponse();
            reader = new StreamReader(response.GetResponseStream());
            string line = reader.ReadLine();
            while (line != null) {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            return result.ToString().Split('\n');
        } catch (Exception ex) {
            if (reader != null) {
                reader.Close();
            }
            if (response != null) {
                response.Close();
            }
            downloadFiles = null;
            return downloadFiles;
        }
    }

덧글

댓글 입력 영역