当前位置:网站首页>Unity C # e-learning (VI) -- FTP (I)

Unity C # e-learning (VI) -- FTP (I)

2022-06-25 01:37:00 Handsome_ shuai_

Unity C# E-learning ( 6、 ... and )——FTP( One )

One .FTP The key class

1.NetworkCredential class

        // be used for FTP Set account number and secret communication voucher during transmission 
        NetworkCredential networkCredential = new NetworkCredential("zzs", "zzzsss123");

2.FtpWebRequest class

        // Create a new WebRequest  be used for ftp signal communication 
        FtpWebRequest ftpWebReq = (FtpWebRequest) WebRequest.Create(new Uri("ftp://127.0.0.1/1.jpg"));
        // If it's going on ftp File transfer , Use Abort You can stop 
        ftpWebReq.Abort();
        // Get the stream for uploading GetRequestStream
        Stream s = ftpWebReq.GetRequestStream();
        // obtain ftp Server response 
        FtpWebResponse ftpWebRep = ftpWebReq.GetResponse() as FtpWebResponse;

        //======= Key members =======
        // Set communication credentials 
        ftpWebReq.Credentials = networkCredential;
        // Whether to close when the request is completed ftp Control connection of the server ( The default is true, Don't shut down )
        ftpWebReq.KeepAlive = false;
        // Operation command settings (WebRequestMethods.Ftp There is a corresponding static variable in the static class to set the value )
        ftpWebReq.Method = WebRequestMethods.Ftp.DownloadFile;
        // Whether to use binary to transmit 
        ftpWebReq.UseBinary = true;
        // Rename file 
        ftpWebReq.RenameTo = "111.jpg";

3.FtpWebResponse class

        FtpWebResponse response = ftpWebReq.GetResponse() as FtpWebResponse;
        // close ftpResponse
        if (response == null)
            return;
        response.Close();
        // Returns the stream returned from the server 
        Stream sm = response.GetResponseStream();
        
        
        //===== Key members =====
        //1. Get the length of the received data 
        Debug.Log(response.ContentLength);
        //2. The type of data received 
        Debug.Log(response.ContentType);
        //3. The latest status code issued by the server 
        Debug.Log(response.StatusCode);
        //4. The text of the latest status code issued by the server 
        Debug.Log(response.StatusDescription);
        //5. When establishing a connection before logging in FTP The message sent by the server 
        Debug.Log(response.BannerMessage);
        //6.FTP Message sent by the server at the end of the session 
        Debug.Log(response.ExitMessage);
        //7.FTP The date and time when the file on the server was uploaded and modified 
        Debug.Log(response.LastModified);

Two .FTP Upload

public class Lesson12 : MonoBehaviour
{
    
    private void Start()
    {
    
        try
        {
    
            NetworkCredential networkCredential = new NetworkCredential("zzs", "zzzsss123");
            FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri("ftp://127.0.0.1/pic.jpg")) as FtpWebRequest;
            if (ftpWebRequest == null)
                return;
            // Must be set to null , Otherwise an error  
            ftpWebRequest.Proxy = null;
            ftpWebRequest.Credentials = networkCredential;
            ftpWebRequest.KeepAlive = false;
            ftpWebRequest.UseBinary = true;
            ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
            Stream upLoadStream = ftpWebRequest.GetRequestStream();
            string path = Application.streamingAssetsPath + "/test.jpg";
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
    
                byte[] buffer = new byte[1024];
                int readLength = fs.Read(buffer, 0, buffer.Length);
                while (readLength > 0)
                {
    
                    upLoadStream.Write(buffer,0,readLength);
                    readLength = fs.Read(buffer, 0, buffer.Length);
                }
                fs.Close();
            }
            upLoadStream.Close();
            Debug.Log(" complete !");
        }
        catch (Exception e)
        {
    
            Debug.Log(e);
            return;
        }
    }
}

3、 ... and .FTP Upload package

1. Process implementation

    private IEnumerator UpLoadFile(string fileName, string path, Action action = null)
    {
    
        NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
        FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL) + fileName) as FtpWebRequest;
        if (ftpWebRequest == null)
            yield break;
        ftpWebRequest.Credentials = networkCredential;
        ftpWebRequest.Proxy = null;
        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpWebRequest.KeepAlive = false;
        ftpWebRequest.UseBinary = true;

        Stream ftpRequestStream = ftpWebRequest.GetRequestStream();

        using (FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read))
        {
    
            byte[] buffer = new byte[10240];
            int length = fs.Read(buffer,0,buffer.Length);
            while (length>0)
            {
    
                ftpRequestStream.Write(buffer,0,length);
                length = fs.Read(buffer,0,buffer.Length);
                yield return null;
            }
        }
        ftpRequestStream.Close();  
        action?.Invoke();
    }

2.Task Multithreaded implementation

    public async void UpLoadFileAsync(string fileName, string path, Action action = null)
    {
    
        await Task.Run(() =>
        {
    
            NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
            FtpWebRequest ftpWebRequest = WebRequest.Create(new Uri(FtpURL) + fileName) as FtpWebRequest;
            if (ftpWebRequest == null)
                return;
            ftpWebRequest.Credentials = networkCredential;
            ftpWebRequest.Proxy = null;
            ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpWebRequest.KeepAlive = false;
            ftpWebRequest.UseBinary = true;

            Stream ftpRequestStream = ftpWebRequest.GetRequestStream();

            using (FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read))
            {
    
                byte[] buffer = new byte[10240];
                int length = fs.Read(buffer,0,buffer.Length);
                while (length>0)
                {
    
                    ftpRequestStream.Write(buffer,0,length);
                    length = fs.Read(buffer,0,buffer.Length);
                }
            }
            ftpRequestStream.Close();  
            action?.Invoke();
        });
    }
原网站

版权声明
本文为[Handsome_ shuai_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242116561817.html