从服务器下载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);
}
}