我开发了一个FTP上传文件的服务,在我本机和测试机器都能正常运行,在客户处就是报502 Command not implemented.错误,请各位帮帮看是什么原因。客户机器不能调试代码。
下面是具体代码:// 缓冲大小设置为100kb
private const int FileBuffLength = 1024 * 100; public static bool Upload(string LocalFileName, string ServerFileName, string FTPServer, string ServerFolder, string FTPUser, string FTPPassword)
{
//检测文件是否存在,文件已经存在,则删除该文件
bool eFile = FTPUtility.FtpFileExists(ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword);
if (eFile)
{
FTPUtility.FtpFileDelete(ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword);
} FileInfo fileInf = new FileInfo(LocalFileName); string strUri = "ftp://" + FTPServer; if (!string.IsNullOrEmpty(ServerFolder))
{
ServerFolder = ServerFolder.Trim('/');
if (!string.IsNullOrEmpty(ServerFolder))
{
strUri += "/" + ServerFolder;
}
} //先保存临时文件名
strUri += "/Temp_" + ServerFileName; FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strUri)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; reqFTP.EnableSsl = false; // 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; //Passive模式
reqFTP.UsePassive = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; byte[] buff = new byte[FileBuffLength];
int contentLen; try
{
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
using (FileStream fs = fileInf.OpenRead())
{
// 把上传的文件写入流
using (Stream strm = reqFTP.GetRequestStream())
{
// 每次读文件流的100kb
contentLen = fs.Read(buff, 0, FileBuffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, FileBuffLength);
}
}
} WebResponse response = reqFTP.GetResponse();
response.Close(); //重命名
if (!FtpFileRename(ServerFileName, "Temp_" + ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword))
{
//重命名失败
return false;
} return true;
}
catch (Exception ex)
{
WriteLog.WriteTxt("FTP Upload File Error:" + ex.Message);
return false;
}
finally
{
if (fileInf != null)
{
fileInf = null;
}
} }FTP
下面是具体代码:// 缓冲大小设置为100kb
private const int FileBuffLength = 1024 * 100; public static bool Upload(string LocalFileName, string ServerFileName, string FTPServer, string ServerFolder, string FTPUser, string FTPPassword)
{
//检测文件是否存在,文件已经存在,则删除该文件
bool eFile = FTPUtility.FtpFileExists(ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword);
if (eFile)
{
FTPUtility.FtpFileDelete(ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword);
} FileInfo fileInf = new FileInfo(LocalFileName); string strUri = "ftp://" + FTPServer; if (!string.IsNullOrEmpty(ServerFolder))
{
ServerFolder = ServerFolder.Trim('/');
if (!string.IsNullOrEmpty(ServerFolder))
{
strUri += "/" + ServerFolder;
}
} //先保存临时文件名
strUri += "/Temp_" + ServerFileName; FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strUri)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; reqFTP.EnableSsl = false; // 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; //Passive模式
reqFTP.UsePassive = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length; byte[] buff = new byte[FileBuffLength];
int contentLen; try
{
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
using (FileStream fs = fileInf.OpenRead())
{
// 把上传的文件写入流
using (Stream strm = reqFTP.GetRequestStream())
{
// 每次读文件流的100kb
contentLen = fs.Read(buff, 0, FileBuffLength); // 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, FileBuffLength);
}
}
} WebResponse response = reqFTP.GetResponse();
response.Close(); //重命名
if (!FtpFileRename(ServerFileName, "Temp_" + ServerFileName, FTPServer, ServerFolder, FTPUser, FTPPassword))
{
//重命名失败
return false;
} return true;
}
catch (Exception ex)
{
WriteLog.WriteTxt("FTP Upload File Error:" + ex.Message);
return false;
}
finally
{
if (fileInf != null)
{
fileInf = null;
}
} }FTP
string ftpRemotePath; //目录
string ftpUserID; //用户名
string ftpPassword; //密码
string ftpURI; //地址 /// <summary>
/// 连接FTP
/// </summary>
/// <param name="FtpServerIP">FTP连接地址</param>
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录,若不指定即默认当前目录</param>
/// <param name="FtpUserID">用户名</param>
/// <param name="FtpPassword">密码</param>
public Ftp(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
ftpURI = "ftp://" + ftpServerIP + ":21/" + ftpRemotePath + "/"; //使用23 端口
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filename">文件名</param>
public void Upload(string filename)
{
FileInfo file = new FileInfo(filename);
//string uri = "ftp://" + ftpServerIP + "/" + file.Name;
string uri = ftpURI + file.Name; FtpWebRequest reqFTP;
//根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
//ftp用户名密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//默认为true ,连接不会被关闭
reqFTP.KeepAlive = false;
//制定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
//执行数据传输类型
reqFTP.UseBinary = true; //上传文件时 通知服务器文件的大小
reqFTP.ContentLength = file.Length; //缓冲大小设置2 kb(够用)
int bufferLength = 2048;
byte[] buff = new byte[bufferLength];
int contenLen; //打开一个文件流 去读上传的文件 FileStream fs = file.OpenRead();
try
{
//把上传的文件写入流
Stream stream = reqFTP.GetRequestStream();
//每次读文件流的2kb
contenLen = fs.Read(buff, 0, bufferLength);
//流内容没有结束
while (contenLen != 0)
{
//把内容从File Stream写入 Upload Stram
stream.Write(buff, 0, contenLen);
contenLen = fs.Read(buff, 0, bufferLength);
}
stream.Close();
fs.Close();
}
catch (Exception ex)
{
throw ex;
}
}
//Passive模式
reqFTP.UsePassive = false;