none
Ftp上传或下载文件是,经常报错The operation has timed out RRS feed

  • 问题

  • Ftp上传或下载文件是,经常报错,错误内容是“The operation has timed out”

    代码如下,请给位大哥帮忙看看是啥问题

    注:由于此方法是Web调用的,应该是存在多线程情况

     

           

    public void SaveToFtp(FtpInfo ftp, string imgFileName, string ftpDir, string imageContent)
    
      {
    
       string step = "0";
    
       try
    
       {
    
        //base64转换为img byte[]
    
        byte[] imgBuffer = Convert.FromBase64String(imageContent);
    
        int contentLen = imgBuffer.Length;
    
    
    
        string strDir = String.Format(@"ftp://{0}/epolice/Sourced/{1}/", ftp.ServerIp, ftpDir);
    
        string strUri = String.Format(@"ftp://{0}/epolice/Sourced/{1}/{2}", ftp.ServerIp, ftpDir, imgFileName);
    
    
    
        step = "1";
    
        FtpWebRequest reqFTP;
    
    
    
        try
    
        {
    
         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strDir));
    
         reqFTP.Credentials = new NetworkCredential(ftp.Username, ftp.Password);
    
         reqFTP.KeepAlive = false;
    
         reqFTP.UsePassive = false;
    
         reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
    
         FtpWebResponse resp = (FtpWebResponse)reqFTP.GetResponse();
    
         resp.Close();
    
        }
    
        catch
    
        {
    
        }
    
        step = "2";
    
        
    
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strUri));
    
        reqFTP.Credentials = new NetworkCredential(ftp.Username, ftp.Password);
    
        reqFTP.KeepAlive = false;
    
        reqFTP.UsePassive = false;
    
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    
        reqFTP.UseBinary = true;
    
        reqFTP.ContentLength = contentLen;
    
        step = "3";
    
        // 把上传的文件写入流
    
        Stream strm = reqFTP.GetRequestStream();
    
        strm.Write(imgBuffer, 0, contentLen);
    
        strm.Close();
    
        step = "4";
    
       }
    
       catch (Exception ex)
    
       {
    
        Logger.WriteLog("FTP:SaveToFtp:" + ex.Message + "步骤:" + step + " 文件名" + imgFileName);
    
        throw new Exception("图片上传失败。");
    
       }
    
      }
    
    
    
      public string LoadFromFtp(FtpInfo ftp, string imgFileName, string ftpDir)
    
      {
    
       string step = "0";
    
       byte[] imgBuffer;
    
       FtpWebRequest reqFTP = null;
    
       FtpWebResponse response = null;
    
       Stream ftpStream = null;
    
       MemoryStream memStream = new MemoryStream();
    
       string strUri = String.Format(@"ftp://{0}/epolice/Sourced/{1}/{2}", ftp.ServerIp, ftpDir, imgFileName);
    
    
    
       try
    
       {
    
        step = "1";
    
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strUri));
    
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    
        reqFTP.KeepAlive = false;
    
        reqFTP.UsePassive = false;
    
        reqFTP.UseBinary = true;
    
        reqFTP.Credentials = new NetworkCredential(ftp.Username, ftp.Password);
    
        step = "2";
    
        response = (FtpWebResponse)reqFTP.GetResponse();
    
        ftpStream = response.GetResponseStream();
    
        step = "3";
    
    
    
        imgBuffer = new byte[10240];
    
        int bytesRead;
    
        while (true)
    
        {
    
         bytesRead = ftpStream.Read(imgBuffer, 0, imgBuffer.Length);
    
         if (bytesRead == 0)
    
          break;
    
         memStream.Write(imgBuffer, 0, bytesRead);
    
        }
    
        step = "4";
    
        imgBuffer = memStream.ToArray();
    
       }
    
       catch (Exception ex)
    
       {
    
        Logger.WriteLog("FTP:LoadFromFtp:" + ex.Message + "步骤:" + step + " 文件名" + imgFileName);
    
        throw new Exception("图片下载失败。");
    
       }
    
       finally
    
       {
    
        if (memStream != null)
    
         memStream.Close();
    
        if (ftpStream!=null)
    
         ftpStream.Close();
    
        if (response != null)
    
         response.Close();
    
       }
    
       return Convert.ToBase64String(imgBuffer);
    
    
    
      }
    
    
    
    
    貌似出现在这句话最多
    response = (FtpWebResponse)reqFTP.GetResponse();

     


    • 已编辑 Phil Gu 2011年5月13日 3:54
    2011年4月20日 6:28

答案

  • 你好

    或者你可以嘗試設定 TIMEOUT properties

    E.G.

    request.Timeout = 600000;
    request.ReadWriteTimeout = 600000;

    Please correct me if my concept is wrong


    Chi
    • 已建议为答案 mazhou 2011年4月29日 10:23
    • 已标记为答案 ChiYauModerator 2012年4月15日 7:08
    2011年4月20日 9:33
    版主

全部回复

  • 可能是server端限制了连接时间
    http://feiyun0112.cnblogs.com/
    2011年4月20日 8:47
    版主
  • 我检查过,没有限制时间和连接数,而且连接数量并发时不超过30个。文件大小也不会超过2M,且在局域网内的系统

    2011年4月20日 9:22
  • 你好

    或者你可以嘗試設定 TIMEOUT properties

    E.G.

    request.Timeout = 600000;
    request.ReadWriteTimeout = 600000;

    Please correct me if my concept is wrong


    Chi
    • 已建议为答案 mazhou 2011年4月29日 10:23
    • 已标记为答案 ChiYauModerator 2012年4月15日 7:08
    2011年4月20日 9:33
    版主
  • 其实我很想知道为什么会出现这个情况,而不是通过设置Timeout来解决超时。

    我猜想是发生了阻塞,究竟是什么原因,还是无法排查

    2011年4月21日 15:19
  • 你好

    有沒有嘗試用 正常的FTP CLIENT 來 CONNECT AND DOWNLOAD 這些FILE

    如果他們都有TIMEOUT 或 DOWNLOAD /CONNECTION 用很多的時間的話..

    那使會是 CONNECTION/NETWORK 的問題了

    我試過自己的 FTP CODE..沒有出現過你的問題

     


    Chi
    2011年4月21日 15:38
    版主
  • 因为是随机出现,所以还不知道用什么办法再现这个异常。之前好像还没这种现象,后来也不知道自己改了那些,就出现了这个问题。

    是KeepAlive属性吗?还是UsePassive ?

    2011年4月24日 15:16
  • 貌似多数出现在

    response = (FtpWebResponse)reqFTP.GetResponse();
            ftpStream = response.GetResponseStream();
    这2句话


    已经确定下来,在response = (FtpWebResponse)reqFTP.GetResponse();这句话上了,具体就是GetResponse方法上
    2011年4月25日 2:23
  • 我想我应该都是关闭的,见上面的代码。

     

    2011年4月25日 7:07
  • 我也遇到了这样的问题,下载时超时,但是我写了个控制台程序却没事。

    目前还不知道实质原因。

    2011年4月29日 2:24
  • 出现这个问题的原因是因为 .NET 中的 WebRequest 类型会有一个默认的连接的 Idle 时间,如果我没有记错,它是 30 秒。如果 30 秒内 GetResponseStream() 没有返回的话,WebRequest 就会抛 TimeoutException 的,这个与服务器无关的。
    Mark Zhou
    2011年4月29日 10:25
  • 问题是什么情况下导致了GetResponse超时30秒呢?无论是文件大小、用户并发连接数,都不太可能导致这种情况。
    2011年5月3日 2:29
  • 我做了一个exe的窗体程序,启动了10个BackgroundWorker 并每个BackgroundWorker 循环调用LoadFromFtp方法10000次,尽然没有任何问题

    但是上述程序在Web中,就会频繁出现响应超时的问题

    我查了一下后台ftp的会话

    发现exe的程序在后台是10个会话,而web调用缺始终只有2个。

    我猜测是web调用最大并发只能2个?那么通过那些参数可以修改这个值呢??

    2011年5月13日 4:37
  • 你好,我是一个学生,正在做毕业设计,做的就是用C#变一套FTP客户端/服务器系统,想参考一下你的程序,能把项目文件发给我一个吗,只有程序也可以,我看一下界面是如何布置的,现在一点头绪都摸不到,谢谢了!


    我的邮箱是chunzhongchun@126.com
    2011年5月17日 7:16
  • 抱歉,由于是公司代码,我私人是不可以外泄的。


    你可以参考以下网页,或许对你有帮助 http://blog.csdn.net/jclass/archive/2010/12/17/6083094.aspx
    2011年5月21日 12:43