用.net 2.0的FtpWebRequest类来上传文件,ftp server是serv-u 6.0版,传输小于12,484,608 字节的文件是成功的(上传正确),可是大于这个大小的文件就断了,错误提示:..."放弃了一个已建立的连接",意思就是ftp连接关闭了。不知道这个问题在哪里,怎么解决,请帮我。代码如下:private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); // Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// reqFTP.KeepAlive = true; // Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type.
reqFTP.UseBinary = true; // Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length; // The buffer size is set to 2kb
// int buffLength = 2048;
int buffLength = 1024;
byte[] buff = new byte[buffLength];
int contentLen; // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength); // Till Stream content ends
while (contentLen != 0)
// int count = 0;
// while ((count = strm.Read(buff, 0, 1024)) > 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
// strm.Write(buff, 0, count);
contentLen = fs.Read(buff, 0, buffLength);
} // Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); // Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// reqFTP.KeepAlive = true; // Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type.
reqFTP.UseBinary = true; // Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length; // The buffer size is set to 2kb
// int buffLength = 2048;
int buffLength = 1024;
byte[] buff = new byte[buffLength];
int contentLen; // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength); // Till Stream content ends
while (contentLen != 0)
// int count = 0;
// while ((count = strm.Read(buff, 0, 1024)) > 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
// strm.Write(buff, 0, count);
contentLen = fs.Read(buff, 0, buffLength);
} // Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货