读书人

从服务器下载FTP文件出现有关问题

发布时间: 2014-04-23 16:14:37 作者: rapoo

从服务器下载FTP文件出现问题
问题如下:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()

各位大虾,这会不会是服务器内存不足导致呢?代码如下


public void FTPFile()
{
int i = 0;
int k = 0;
string temp_strServer = "ftp://××××";
string temp_remoteFile = "XXX.tab";
string temp_userID = "×××";
string temp_passWord = "×××";
Uri temp_URI = new Uri(temp_strServer + temp_remoteFile);
string temp_localFilePath = Application.StartupPath + "/temp/";

try
{
currentTime = DateTime.Now;
WriteLog("---begin FTP---" + currentTime);

FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(temp_URI);
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
ftpReq.UseBinary = true;
ftpReq.Credentials = new NetworkCredential(temp_userID, temp_passWord);
FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();
StreamReader ftpRespStream = new StreamReader(ftpResp.GetResponseStream());
// Stream ftpRespStream = ftpResp.GetResponseStream();

string line = ftpRespStream.ReadLine();
while (line != null)
{
FileInfo myfileinfo = new FileInfo(temp_localFilePath + "XXX_" + i.ToString() + ".tab");
if (!myfileinfo.Exists)
{
FileStream fs = myfileinfo.Create();
fs.Close();
}
StreamWriter mysw = myfileinfo.AppendText();
mysw.WriteLine(line);
mysw.Close(); // have to be closed



k = k + 1;
if (k > 20000)
{
k = 0;
myfileinfo.MoveTo(temp_localFilePath + "XXX_" + i.ToString() + "_2.tab");
currentTime = DateTime.Now;
WriteLog("---complete one file---file No.: " + i + "----- at :" + currentTime);
i = i + 1;
}

line = ftpRespStream.ReadLine();
//if (i == 2 && k == 100) // for test
//{
// line = null;
//}
}
FileInfo myfileinfo2 = new FileInfo(temp_localFilePath + "XXX_" + i.ToString() + ".tab");
if (myfileinfo2.Exists)
{
myfileinfo2.MoveTo(temp_localFilePath + "XXX_" + i.ToString() + "_2.tab");
}
ftpResp.Close();
g_completeFTP = true;
currentTime = DateTime.Now;
WriteLog("---complete all FTP files------------file No.: " + i + "---------- at :" + currentTime);
}
catch (Exception e)
{
WriteLog(e.ToString());
g_completeFTP = true;
}
}
[解决办法]
各位大虾,这会不会是服务器内存不足导致呢?

这种可能性最大
[解决办法]
你下载的文件有多大。。

public void Download(string filePath, string fileName)  
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;


reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
}
}


[解决办法]
An existing connection was forcibly closed by the remote host.

远程连接被强迫关闭了,你试试使用ftp软件能否下载
[解决办法]
本帖最后由 net_lover 于 2011-08-12 11:28:28 编辑 如果文件大。你可以使用异步下载
BeginGetRequestStream

或者WebClient异步下载

参见
http://stuff.seans.com/2009/01/05/using-httpwebrequest-for-asynchronous-downloads/
[解决办法]
最好能看到ftp服务器端的日志,可以清楚的看到哪条ftp指令出了问题

读书人网 >.NET Framework

热点推荐