728x90
반응형
using UnityEngine;
using System.Net;
using System.IO;
public class FTP : MonoBehaviour
{
public string m_UserName = "id@site.com";
public string m_Password = "abcde";
void Update()
{
if (Input.GetKeyDown(KeyCode.U))
FtpUpload();
if (Input.GetKeyDown(KeyCode.D))
FtpDownload();
}
void FtpUpload()
{
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.site.com/upload.txt");
ftpWebRequest.Credentials = new NetworkCredential(m_UserName, m_Password);
//ftpWebRequest.EnableSsl = true; // TLS/SSL
ftpWebRequest.UseBinary = false; // ASCII, Binary(디폴트)
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
byte[] data = File.ReadAllBytes(@"D:\upload.txt");
using (var ftpStream = ftpWebRequest.GetRequestStream())
{
ftpStream.Write(data, 0, data.Length);
}
using (var response = (FtpWebResponse)ftpWebRequest.GetResponse())
{
Debug.Log(response.StatusDescription);
}
}
void FtpDownload()
{
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.site.com/download.txt");
ftpWebRequest.Credentials = new NetworkCredential(m_UserName, m_Password);
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
using (var localfile = File.Open(@"D:\download.txt", FileMode.Create))
using (var ftpStream = ftpWebRequest.GetResponse().GetResponseStream())
{
byte[] buffer = new byte[1024];
int n;
while ((n = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
localfile.Write(buffer, 0, n);
}
}
}
}
728x90
반응형
'프로그램 > 유니티 네트워크' 카테고리의 다른 글
[유니티 네트워크] 파일 전송(File Transfer) (0) | 2021.02.02 |
---|---|
[유니티 네트워크] 멀티 스레드 1:N TcpServer, TcpClient 문자열 전송 (TcpListener, TcpClient) (0) | 2021.02.02 |
[유니티 네트워크] TcpServer, TcpClient 구조체 전송 (Socket) (0) | 2020.08.05 |
[유니티 네트워크] Multicast Sender, Multicast Receiver 구조체 전송 (UdpClient) (1) | 2020.08.05 |
[유니티 네트워크] Multicast Sender, Multicast Receiver 구조체 전송 (Socket) (0) | 2020.08.05 |