代码:
          //创建文件夹
            if (!result) MakeDirectory(remoteSaveFilePath);
            FtpWebResponse response = null;
            FtpWebRequest req = null;
            Stream stream = null;
            FileStream fs = null;
            try
            {
                FileInfo fi = new FileInfo(localFile);                Uri _uri = new Uri(uriStr + "/" + remoteSaveFilePath + "/" + fi.Name);                ///上传                req = GetFtpWebRequest(WebRequestMethods.Ftp.UploadFile, _uri, ftp_uid, ftp_pwd);                req.ContentLength = fi.Length;                response = (FtpWebResponse)req.GetResponse();                int buffLength = 1024;                byte[] buff = new byte[buffLength];                int contentLen;                fs = fi.Open(FileMode.OpenOrCreate);                stream = req.GetRequestStream();                contentLen = fs.Read(buff, 0, buffLength);                while (contentLen != 0)
                {
                    stream.Write(buff, 0, contentLen);                    contentLen = fs.Read(buff, 0, buffLength);
                }                stream.Close();                fs.Close();                response.Close();                Thread.Sleep(10);
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    fs.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                return false;
                throw;
            }
            return true;[code=csharp][/code]
情况:第一上传的时候,程序正常运行,第二次再次上传 代码抛出异常 
[图片]
请问各位大神,这种情况是如何发生的?有什么办法去解决他?
没分了,麻烦各位大神告知一下,谢谢。

解决方案 »

  1.   

    贴出来的代码没看出什么毛病,估计问题出在GetFtpWebRequest这个函数上。没看到GetFtpWebRequest是怎么写的,猜想可能是因为第二次调用和每一次调用返回的是同一个FtpWebRequest对象,而FtpWebRequest对象的response在第一次被你关闭了,所以报错了。  226 Transfer complete这个返回明显是第一次上传成功后Ftp服务顺的返回信息。另外,你既然在catch里throw了,那为什么不把所有的close()语句写在finally块里或直接用using?
      

  2.   

      => 猜想可能是因为第二次调用和第一次调用返回的是同一个FtpWebRequest对象
       => 226 Transfer complete这个返回明显是第一次上传成功后Ftp服务器的返回信息。
      

  3.   

    看看是否是这种情况 https://support.microsoft.com/en-us/help/2685051/226-transfer-complete-message-when-you-run-the-ls-or-dir-command-again226 Transfer complete 本意是传输完成
    但含义并不明确,一般服务端发生接收超时也是此信号
      

  4.   


    代码:
      /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public FtpWebRequest GetFtpWebRequest(string method, Uri _uri = null, string _uid = null, string _pwd = null,bool KeepAlive = true,bool UseBinary = true, bool UsePassive = true)
            {
                try
                {
                    Uri uri_ = _uri == null ? uri : _uri;                string uid_ = _uid == null ? ftp_uid : _uid;                string pwd_ = _pwd == null ? ftp_pwd : _pwd;                FtpWebRequest ftpReq = null;                ftpReq = (FtpWebRequest)FtpWebRequest.Create(uri_);                ftpReq.UseBinary = UseBinary;                ftpReq.KeepAlive = KeepAlive;                ftpReq.UsePassive = UsePassive;                if (isCredential)
                        ftpReq.Credentials = new NetworkCredential(uid_, pwd_);                 ftpReq.Method = method;                return ftpReq;
                }
                catch (Exception ex)
                {
                    log = log4net.LogManager.GetLogger("FtpHelper_Mrh - GetFtpWebRequest");
                    log.Error(ex.Message);
                    throw ex;
                }
            }
     ftpReq.KeepAlive = true
    设置了长连接就没问题。有可能是xuzuning 版主说的问题。还没测试,打了补丁后测试再看下