eagleshi 发表于 2015-5-27 09:53:44

ftp上传文件以及发命令

  1.ftpweb.cs是将文件以ftp的形式上传到ftp服务器;
  eg: FtpWeb ftpUpload = new FtpWeb("202.105.49.15", "", "aaaa", "aaaa556");
      ftpUpload.Upload("D:\\test.txt");
  2.ftpClient类,完成文件的上传,以及向服务器发送命令,并接收服务器返回的信息,根据返回信息做相应处理;
  eg:
  
  ///
      /// 上传文件到ftp服务器,如果成功返回"1",网盘磁盘空间不够返回"-1",否则返回出错信息;
      ///
      ///
      ///
      ///
      ///
      public string uploadFileToFtpServer(UploadFile upFile, string username, string filePath)
      {
            string errMsg = string.Empty;
            string replyMsg = string.Empty;
            try
            {
                FileInfo file = new FileInfo(filePath);
                if (file == null)
                {
                  errMsg += "登录网盘->上传的文件不存在";
                  return errMsg;
                }
  FTPClient ftpc = new FTPClient(upFile.FtpServerIp, upFile.FtpServerPort);
                ftpc.login(username, upFile.AuthCode);
  errMsg += "登录网盘->登录成功(" + DateTime.Now.ToString() + ")";
                string[] arrAllo = { "213", "501" };
  
  //检查ftp的容量;
                replyMsg = ftpc.quote("allo", arrAllo);
                //返回"213 可用空间大小"
                errMsg += "登录网盘->查询服务器容量,返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")";
                int quot = Convert.ToInt32(replyMsg.Split(' '));
  if (quot < file.Length)
                {
                  errMsg += "登录网盘->网盘空间不够,当前文件大小为:" + file.Length + " (" + DateTime.Now.ToString() + ")";
                  //退出FTP服务器
                  ftpc.quit();
                  return "-1";
                }
  ftpc.connectmode= FTPConnectMode.PASV;
                ftpc.transfertype= FTPTransferType.BINARY;
                //上传文件;
                ftpc.put(filepath, upFile.FileId);
                //校验文件
                string[] arrCRC = { "900", "901" };
                string strCRC = "CRC21 " + upFile.FileId + " " + file.Length.ToString();
                replyMsg = ftpc.quote(strcrc, null);
  errMsg += "登录网盘->校验文件返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")";
  //轮询检查文件是否上传完成
                string code = string.Empty;
                string[] arrChkcmp = { "900", "902", "904" };
                string strChk = "Chkcmp " + upFile.FileId;
  while (true)
                {
  replyMsg = ftpc.quote(strchk, arrChkcmp);
             errMsg += "登录网盘->轮询检查文件是否上传完成,返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")";
  code = replyMsg.Substring(0, 3);
                  if (code == "900")
                  {
                        break;
                  }
                  else
                  {
                        Thread.Sleep(30000);
                  }
                }
                errMsg += "登录网盘->流程完毕,断开连接!(" + DateTime.Now.ToString() + ")";
            
               //退出FTP服务器
                ftpc.quit();
                     }
            catch(Exception e)
            {
                           return errMsg +"异常信息:"+ e.Message;
         
               
            }
            return "1";   
      }
  
页: [1]
查看完整版本: ftp上传文件以及发命令