代码:
//创建文件夹
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]
情况:第一上传的时候,程序正常运行,第二次再次上传 代码抛出异常
[图片]
请问各位大神,这种情况是如何发生的?有什么办法去解决他?
没分了,麻烦各位大神告知一下,谢谢。
//创建文件夹
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]
情况:第一上传的时候,程序正常运行,第二次再次上传 代码抛出异常
[图片]
请问各位大神,这种情况是如何发生的?有什么办法去解决他?
没分了,麻烦各位大神告知一下,谢谢。
=> 226 Transfer complete这个返回明显是第一次上传成功后Ftp服务器的返回信息。
但含义并不明确,一般服务端发生接收超时也是此信号
代码:
/// <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 版主说的问题。还没测试,打了补丁后测试再看下