积极答复者
Ftp上传或下载文件是,经常报错The operation has timed out

问题
-
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
答案
全部回复
-
可能是server端限制了连接时间
http://feiyun0112.cnblogs.com/ -
你好,我是一个学生,正在做毕业设计,做的就是用C#变一套FTP客户端/服务器系统,想参考一下你的程序,能把项目文件发给我一个吗,只有程序也可以,我看一下界面是如何布置的,现在一点头绪都摸不到,谢谢了!
我的邮箱是chunzhongchun@126.com -
抱歉,由于是公司代码,我私人是不可以外泄的。
你可以参考以下网页,或许对你有帮助 http://blog.csdn.net/jclass/archive/2010/12/17/6083094.aspx