Пример #1
0
        public string GetWorkingDirectory()
        {
            // PWD - print working directory
            Connect();
            SendCommand("PWD");
            myFTPResponse = GetFTPResponse();

            if (myFTPResponse.Status != 257)
            {
                throw new Exception(myFTPResponse.Message);
            }

            string pwd;

            try
            {
                pwd = myFTPResponse.Message.Substring(myFTPResponse.Message.IndexOf("\"", 0) + 1); //5;
                pwd = pwd.Substring(0, pwd.LastIndexOf("\""));
                pwd = pwd.Replace("\"\"", "\"");                                                   // directories with quotes in the name come out as "" from the server
            }
            catch (Exception ex)
            {
                CloseConnect();
                throw new Exception("Uhandled PWD response: " + ex.Message);
            }
            CloseConnect();
            return(pwd);
        }
Пример #2
0
 public void RemoveFile(string fileName)
 {
     Connect();
     SendCommand("DELE " + fileName);
     myFTPResponse = GetFTPResponse();
     if (myFTPResponse.Status != 250)
     {
         throw new Exception(myFTPResponse.Message);
     }
 }
Пример #3
0
 /**
  * 修改 删除 创建 重命名
  */
 public void ChangeDir(string path)
 {
     Connect();
     this.SendCommand("CWD " + path);
     myFTPResponse = GetFTPResponse();
     if (myFTPResponse.Status != 250)
     {
         throw new Exception(myFTPResponse.Message);
     }
 }
Пример #4
0
 public void RemoveDir(string dir)
 {
     Connect();
     SendCommand("RMD " + dir);
     myFTPResponse = GetFTPResponse();
     if (myFTPResponse.Status != 250)
     {
         throw new Exception(myFTPResponse.Message);
     }
 }
Пример #5
0
        /*
         * 上传部分的实现(已实现断点续传)
         * 首先需要搞清楚本地文件上传了多少,然后转告服务器
         * 利用RETR命令即可实现
         * 但是有可能在发起续传的时候,服务器端文件已经发生更新,那么此时必须重新开始传输
         * 因此需要比对本地文件的修改时间与远程的修改时间
         * 解决方法:将缓存文件命名为:<文件名>.tmp
         * 在文件开头插入时间戳
         */

        public long GetFileSize(string filename)
        {        //获取远程文件大小
            Connect();
            SendCommand("SIZE " + filename);
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 213)
            {
                throw new Exception(myFTPResponse.Message);
            }
            return(long.Parse(myFTPResponse.Message.Substring(4)));
        }
Пример #6
0
        /*
         * 获取文件目录
         */

        /*
         * 获取文件目录(返回列表)
         *  -r--r--r-- 1 ftp ftp         202752 Apr 16  2020 18级os设计讲稿.doc
         *      -r--r--r-- 1 ftp ftp        2115458 May 22  2020 2018302110186-吴轲-操作系统实验 报告.docx
         *      drwxr-xr-x 1 ftp ftp              0 Jul 24 23:00 619
         *      drwxr-xr-x 1 ftp ftp              0 Jul 24 23:00 624
         *      drwxr-xr-x 1 ftp ftp              0 Jul 24 23:00 723
         */

        public string GetList()
        {
            byte[]    Recbufs = new byte[1024];
            byte[]    bufs    = new byte[1030];
            int       receiveBytes;
            string    fileList = "";
            ArrayList list     = new ArrayList();

            Connect();
            OpenDataSock();
            SendCommand("LIST -l");
            myFTPResponse = GetFTPResponse();

            if (myFTPResponse.Status != 150 && myFTPResponse.Status != 125)
            {
                Fail(myFTPResponse);
            }
            int tmp = 0;

            while (DataSock.Available > 0)
            {
                receiveBytes = DataSock.Receive(Recbufs, Recbufs.Length, 0);
                for (int i = 0; i < receiveBytes; i++)
                {
                    bufs[i + tmp] = Recbufs[i];
                }
                tmp = 0;
                string tmpList = ecd.GetString(bufs, 0, receiveBytes);

                while (tmpList[tmpList.Length - 1] == 65533)
                {
                    tmp++;
                    tmpList = ecd.GetString(bufs, 0, receiveBytes - tmp);
                }
                for (int i = 0; i < tmp; i++)
                {
                    bufs[i] = Recbufs[receiveBytes - tmp + i];
                }
                fileList += tmpList;
                System.Threading.Thread.Sleep(50);
            }

            CloseDataSock();
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 226)
            {
                Fail(myFTPResponse);
            }

            CloseConnect();
            return(fileList);
        }
Пример #7
0
        public string GetDate(string fileName)
        {        //从远程服务器获取所需文件的日期
            Connect();

            SendCommand("MDTM " + fileName);
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 213)
            {
                throw new Exception(myFTPResponse.Message);
            }

            return(Regex.Replace(myFTPResponse.Message.Substring(4), "\r", ""));
        }
Пример #8
0
        //打开DataSocket
        private void OpenDataSock()
        {
            Connect();
            SendCommand("PASV");
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 227)
            {
                Fail(myFTPResponse);
            }

            //判断227回应是否正常

            string[] pasvResponse;
            try
            {
                string tmp = Regex.Replace(myFTPResponse.Message, "(.*)(\\()(.*)(\\))(.*)", "$3");
                pasvResponse = tmp.Split(',');
                if (pasvResponse.Length < 6)
                {
                    throw new Exception($"PASV回应非法!({ myFTPResponse.Message })");
                }
            }
            catch
            {
                CloseConnect();
                throw new Exception($"PASV回应非法!({ myFTPResponse.Message })");
            }

            string server = string.Format("{0}.{1}.{2}.{3}", pasvResponse[0], pasvResponse[1], pasvResponse[2], pasvResponse[3]);
            int    port   = (int.Parse(pasvResponse[4]) << 8) + int.Parse(pasvResponse[5]);

            //建立DataSocket
            try
            {
                //先关闭已有的Socket
                CloseDataSock();

                //创建新的DataSocket
                DataSock       = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                DataIPEndPoint = new IPEndPoint(Dns.GetHostByName(server).AddressList[0], port);

                //连接
                DataSock.Connect(DataIPEndPoint);
            }
            catch (Exception ex)
            {
                throw new Exception("无法建立数据传输! " + ex.Message);
            }
        }
Пример #9
0
        public void MakeDir(string dir)
        {
            Connect();
            this.SendCommand("MKD " + dir);
            myFTPResponse = GetFTPResponse();

            switch (myFTPResponse.Status)
            {
            case 257:
            case 250:
                break;

            default:
                throw new Exception(myFTPResponse.Message);
            }
        }
Пример #10
0
        /*
         * 下载部分的实现(已实现断点续传)
         * 首先需要搞清楚本地文件下载了多少,然后转告服务器
         * 利用RETR命令即可实现
         * 但是有可能在发起续传的时候,服务器端文件已经发生更新,那么此时必须重新开始传输
         * 因此需要比对本地文件的修改时间与远程的修改时间
         * 解决方法:将缓存文件命名为:<文件名>.tmp
         * 在文件开头插入时间戳
         */

        //设置二进制模式
        private void SetBinaryMode(bool mode)
        {
            if (mode)
            {
                SendCommand("TYPE I");
            }
            else
            {
                SendCommand("TYPE A");
            }

            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 200)
            {
                Fail(myFTPResponse);
            }
        }
Пример #11
0
 public void RenameDir(string oldName, string newName)
 {
     Connect();
     SendCommand("RNFR " + oldName);
     myFTPResponse = GetFTPResponse();
     if (myFTPResponse.Status != 350)
     {
         CloseConnect();
         throw new Exception(myFTPResponse.Message);
     }
     SendCommand("RNTO " + newName);
     myFTPResponse = GetFTPResponse();
     if (myFTPResponse.Status != 250)
     {
         throw new Exception(myFTPResponse.Message);
     }
 }
Пример #12
0
        //构造FTP对象:传入参数为服务器地址+端口号+用户名+密码
        public MyFTP(string server, int port, string user, string pass)
        {
            this.Server   = server;
            this.UserName = user;
            this.Password = pass;
            this.Port     = port;

            this.MainSock       = null;
            this.MainIPEndPoint = null;
            this.ecd            = Encoding.UTF8;

            this.DataSock       = null;
            this.DataIPEndPoint = null;
            this.file           = null;

            this.myFTPResponse = new MyFTPResponse();
            this.BufferPool    = "";
        }
Пример #13
0
        public MyFTP()
        {
            this.Server   = null;
            this.UserName = null;
            this.Password = null;
            this.Port     = 21;        //默认端口为21。

            this.MainSock       = null;
            this.MainIPEndPoint = null;
            this.ecd            = Encoding.UTF8;

            this.DataSock       = null;
            this.DataIPEndPoint = null;
            this.file           = null;

            this.myFTPResponse = new MyFTPResponse();
            this.BufferPool    = "";
        }
Пример #14
0
        public MyFTPResponse GetFTPResponse()
        {
            string rtn = "";

            while (true)
            {
                string buf = GetLine();
                // "000 This is the end of the response"
                if (Regex.Match(buf, "^[0-9]+ ").Success)
                {
                    MyFTPResponse tmp = new MyFTPResponse(buf);
                    return(new MyFTPResponse(tmp.Status, rtn + tmp.Message));
                }
                else
                {
                    rtn += buf;
                }
            }
        }
Пример #15
0
        //DownLoadFile("D:\\in.txt",string "in.txt")
        public void DownLoadFile(string LocalDirName, string RemotefileName, int StartOffset = 14)
        {
            //首先建立下载请求
            Connect();
            SetBinaryMode(true);
            OpenDataSock();
            file = new FileStream(LocalDirName, FileMode.Append);
            SendCommand("REST " + (file.Length - StartOffset).ToString());
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 350)
            {
                CloseDataSock();
                file = null;
                throw new Exception(myFTPResponse.Message);
            }

            SendCommand("RETR " + RemotefileName);
            myFTPResponse = GetFTPResponse();

            if (myFTPResponse.Status != 125 && myFTPResponse.Status != 150)
            {
                file = null;
                throw new Exception(myFTPResponse.Message);
            }

            //开始下载
            byte[] bytes = new byte[4096];
            long   bytesGot;

            while (true)
            {
                try
                {
                    bytesGot = DataSock.Receive(bytes, bytes.Length, 0);
                    if (bytesGot <= 0)
                    {
                        CloseDataSock();
                        file.Close();
                        myFTPResponse = GetFTPResponse();
                        if (myFTPResponse.Status != 250 && myFTPResponse.Status != 226)
                        {
                            throw new Exception(myFTPResponse.Message);
                        }

                        SetBinaryMode(false);
                        return;
                    }
                    file.Write(bytes, 0, (int)bytesGot);
                }
                catch (Exception ex)
                {
                    CloseDataSock();
                    if (file != null)
                    {
                        file.Close();
                        file = null;
                    }
                    GetFTPResponse();
                    SetBinaryMode(false);
                    throw (ex);
                }
            }
        }
Пример #16
0
        /*
         * Socket相关
         * (默认使用被动模式)
         * DataSock的open,connect,close
         * MainSock的open,connect,close
         */

        public void Connect()
        {
            //验证用户名与服务器是否为空
            if (this.Server == null)
            {
                throw new Exception("服务器为空!");
            }

            if (this.UserName == null)
            {
                throw new Exception("用户名为空!");
            }
            //如果已经连上,则不用做任何事情。
            if (this.MainSock != null)
            {
                if (this.MainSock.Connected)
                {
                    return;
                }
            }

            //连接到FTP服务器
            this.MainSock       = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.MainIPEndPoint = new IPEndPoint(Dns.GetHostByName(this.Server).AddressList[0], this.Port);
            //this.MainSock.Bind(ipEndPoint);

            try
            {
                this.MainSock.Connect(this.MainIPEndPoint);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 220)
            {
                this.Fail(myFTPResponse);
            }

            this.SendCommand("USER " + this.UserName);
            myFTPResponse = GetFTPResponse();

            switch (myFTPResponse.Status)
            {
            case 331:
                if (this.Password == null)
                {
                    this.CloseConnect();
                    throw new Exception("No password has been set.");
                }

                this.SendCommand("PASS " + this.Password);
                myFTPResponse = GetFTPResponse();
                if (myFTPResponse.Status != 230)
                {
                    this.Fail(myFTPResponse);
                }

                break;

            case 230:
                break;
            }

            return;
        }
Пример #17
0
        public void UploadFile(string LocalfileName, string RemotefileName, int StartOffset = 18)
        {
            //首先建立上传请求
            Connect();
            SetBinaryMode(true);
            OpenDataSock();
            //获取远程文件的大小
            long sizeReceived;

            SendCommand("SIZE " + RemotefileName);
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 213)
            {
                if (myFTPResponse.Status == 550)
                {
                    sizeReceived = 0;
                }
                else
                {
                    throw new Exception(myFTPResponse.Message);
                }
            }
            else
            {
                sizeReceived = long.Parse(myFTPResponse.Message.Substring(4));
            }

            file = new FileStream(LocalfileName, FileMode.Open);
            file.Seek(StartOffset + sizeReceived, SeekOrigin.Begin);

            SendCommand("APPE " + RemotefileName);
            myFTPResponse = GetFTPResponse();
            if (myFTPResponse.Status != 150 && myFTPResponse.Status != 125)
            {
                throw new Exception(myFTPResponse.Message);
            }

            byte[] buf = new byte[8192];
            int    bytes;

            while (true)
            {
                try
                {
                    bytes = file.Read(buf, 0, buf.Length);
                    DataSock.Send(buf, bytes, 0);

                    if (bytes <= 0)
                    {
                        file.Close();
                        file = null;
                        CloseDataSock();
                        myFTPResponse = GetFTPResponse();
                        if (myFTPResponse.Status != 250 && myFTPResponse.Status != 226)
                        {
                            throw new Exception(myFTPResponse.Message);
                        }

                        SetBinaryMode(false);
                        return;
                    }
                }catch (Exception ex)
                {
                    if (file != null)
                    {
                        file.Close();
                    }
                    file = null;
                    CloseDataSock();
                    GetFTPResponse();
                    throw new Exception(ex.Message);
                }
            }
        }
Пример #18
0
 //链接异常的处理
 private void Fail(MyFTPResponse myFTPResponse)
 {
     this.CloseConnect();
     throw new Exception(myFTPResponse.Message);
 }