运行了很长一段时间一直没有问题,最近发现无法上传。
错误内容:
==========================================================
System.Net.WebException: 基础连接已经关闭: 接收时发生错误。
在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
在 System.Net.FtpWebRequest.RequestCallback(Object obj)
在 System.Net.CommandStream.Abort(Exception e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
在 System.Net.FtpWebRequest.GetRequestStream()
在 FTPAPI.FTPAPI.transFileToFTP(String filePathp_source, String filePathp_aim, String uploadURIp, String userNamep, String passwordp, String EncryptKey, Exception& e)
==========================================================
测试发现只有特定一些路径上传时失败,在
Stream uploadStream = transFileRequest.GetRequestStream()
时抛异常。FTP服务器断日志发现登陆已完成,并且已经切换目录完成,之后就直接断开了。代码如下:public static bool transFileToFTP(string filePathp_source, string filePathp_aim, string uploadURIp, string userNamep, string passwordp, string EncryptKey, out Exception e)
{
//userNamep = ConvertUserName(userNamep, CONVERT_USER_EXTENSION);
//passwordp = ConvertPasswd(passwordp, CONVERT_PASS_EXTENSION);
try
{
Exception aa = new Exception();
FileInfo sourceFileInfo = new FileInfo(filePathp_source);
FtpWebRequest transFileRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + uploadURIp + "/" + filePathp_aim));
transFileRequest.Credentials = new NetworkCredential(userNamep, passwordp);
transFileRequest.KeepAlive = false;
transFileRequest.UseBinary = true;
transFileRequest.Method = WebRequestMethods.Ftp.UploadFile;
transFileRequest.ContentLength = sourceFileInfo.Length;
byte[] buffer = new byte[2048];//指定缓存
int bufferCount = 0;
int contentLength;
using (FileStream fs = sourceFileInfo.OpenRead())//将本地文件读入到流中
{
// 获取已上传部分文件大小
long existedFileSize = GetFileSize(filePathp_aim, uploadURIp, userNamep, passwordp);
// 本地流中远程文件已经写入的部分大小
long repeatDataLength = 0;
using (Stream uploadStream = transFileRequest.GetRequestStream())//得到ftp服务器返回的流
{ //--将从本地文件的流中读出的数据写入ftp服务器返回的流中--
contentLength = fs.Read(buffer, 0, buffer.Length);
long filePos = 0;
while (contentLength != 0)
{
if (repeatDataLength > 0)
{
uploadStream.Write(buffer, (int)repeatDataLength, contentLength - (int)repeatDataLength);
repeatDataLength = 0;
}
else
{
uploadStream.Write(buffer, 0, contentLength);
}
contentLength = fs.Read(buffer, 0, buffer.Length);
filePos += contentLength;
}
try
{
//--将从本地文件的流中读出的数据写入ftp服务器返回的流中--
if (!uploadStream.Equals(null))
{
uploadStream.Flush();
uploadStream.Close();//关闭从ftp服务器返回的流
}
}
catch
{
}
}
try
{
fs.Flush();
fs.Close();//关闭本地文件的流
}
catch
{
}
}
e = aa;
return true;
}
catch (Exception es)
{
//throw new Exception(es.Message);
e = es;
return false;
}
}
错误内容:
==========================================================
System.Net.WebException: 基础连接已经关闭: 接收时发生错误。
在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
在 System.Net.FtpWebRequest.RequestCallback(Object obj)
在 System.Net.CommandStream.Abort(Exception e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
在 System.Net.FtpWebRequest.GetRequestStream()
在 FTPAPI.FTPAPI.transFileToFTP(String filePathp_source, String filePathp_aim, String uploadURIp, String userNamep, String passwordp, String EncryptKey, Exception& e)
==========================================================
测试发现只有特定一些路径上传时失败,在
Stream uploadStream = transFileRequest.GetRequestStream()
时抛异常。FTP服务器断日志发现登陆已完成,并且已经切换目录完成,之后就直接断开了。代码如下:public static bool transFileToFTP(string filePathp_source, string filePathp_aim, string uploadURIp, string userNamep, string passwordp, string EncryptKey, out Exception e)
{
//userNamep = ConvertUserName(userNamep, CONVERT_USER_EXTENSION);
//passwordp = ConvertPasswd(passwordp, CONVERT_PASS_EXTENSION);
try
{
Exception aa = new Exception();
FileInfo sourceFileInfo = new FileInfo(filePathp_source);
FtpWebRequest transFileRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + uploadURIp + "/" + filePathp_aim));
transFileRequest.Credentials = new NetworkCredential(userNamep, passwordp);
transFileRequest.KeepAlive = false;
transFileRequest.UseBinary = true;
transFileRequest.Method = WebRequestMethods.Ftp.UploadFile;
transFileRequest.ContentLength = sourceFileInfo.Length;
byte[] buffer = new byte[2048];//指定缓存
int bufferCount = 0;
int contentLength;
using (FileStream fs = sourceFileInfo.OpenRead())//将本地文件读入到流中
{
// 获取已上传部分文件大小
long existedFileSize = GetFileSize(filePathp_aim, uploadURIp, userNamep, passwordp);
// 本地流中远程文件已经写入的部分大小
long repeatDataLength = 0;
using (Stream uploadStream = transFileRequest.GetRequestStream())//得到ftp服务器返回的流
{ //--将从本地文件的流中读出的数据写入ftp服务器返回的流中--
contentLength = fs.Read(buffer, 0, buffer.Length);
long filePos = 0;
while (contentLength != 0)
{
if (repeatDataLength > 0)
{
uploadStream.Write(buffer, (int)repeatDataLength, contentLength - (int)repeatDataLength);
repeatDataLength = 0;
}
else
{
uploadStream.Write(buffer, 0, contentLength);
}
contentLength = fs.Read(buffer, 0, buffer.Length);
filePos += contentLength;
}
try
{
//--将从本地文件的流中读出的数据写入ftp服务器返回的流中--
if (!uploadStream.Equals(null))
{
uploadStream.Flush();
uploadStream.Close();//关闭从ftp服务器返回的流
}
}
catch
{
}
}
try
{
fs.Flush();
fs.Close();//关闭本地文件的流
}
catch
{
}
}
e = aa;
return true;
}
catch (Exception es)
{
//throw new Exception(es.Message);
e = es;
return false;
}
}
其实使用第三方的FTP客户端访问都木有问题。