ftp上传文件以及发命令
时间:2010-09-06 来源:漠北的天空
1.ftpweb.cs是将文件以ftp的形式上传到ftp服务器;
eg: FtpWeb ftpUpload = new FtpWeb("202.105.49.15", "", "aaaa", "aaaa556");
ftpUpload.Upload("D:\\test.txt");
2.ftpClient类,完成文件的上传,以及向服务器发送命令,并接收服务器返回的信息,根据返回信息做相应处理;
eg:
/// <summary>
/// 上传文件到ftp服务器,如果成功返回"1",网盘磁盘空间不够返回"-1",否则返回出错信息;
/// </summary>
/// <param name="upFile"></param>
/// <param name="username"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public string uploadFileToFtpServer(UploadFile upFile, string username, string filePath)
{
string errMsg = string.Empty;
string replyMsg = string.Empty;
try
{
FileInfo file = new FileInfo(filePath);
if (file == null)
{
errMsg += "登录网盘->上传的文件不存在<br/>";
return errMsg;
}
FTPClient ftpc = new FTPClient(upFile.FtpServerIp, upFile.FtpServerPort);
ftpc.login(username, upFile.AuthCode);
errMsg += "登录网盘->登录成功 (" + DateTime.Now.ToString() + ")<br/>";
string[] arrAllo = { "213", "501" };
//检查ftp的容量;
replyMsg = ftpc.quote("allo", arrAllo);
//返回"213 可用空间大小"
errMsg += "登录网盘->查询服务器容量,返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")<br/>";
int quot = Convert.ToInt32(replyMsg.Split(' ')[1]);
if (quot < file.Length)
{
errMsg += "登录网盘->网盘空间不够,当前文件大小为:" + file.Length + " (" + DateTime.Now.ToString() + ")<br/>";
//退出FTP服务器
ftpc.quit();
return "-1";
}
ftpc.connectmode= FTPConnectMode.PASV;
ftpc.transfertype= FTPTransferType.BINARY;
//上传文件;
ftpc.put(filepath, upFile.FileId);
//校验文件
string[] arrCRC = { "900", "901" };
string strCRC = "CRC21 " + upFile.FileId + " " + file.Length.ToString();
replyMsg = ftpc.quote(strcrc, null);
errMsg += "登录网盘->校验文件返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")<br/>";
//轮询检查文件是否上传完成
string code = string.Empty;
string[] arrChkcmp = { "900", "902", "904" };
string strChk = "Chkcmp " + upFile.FileId;
while (true)
{
replyMsg = ftpc.quote(strchk, arrChkcmp);
errMsg += "登录网盘->轮询检查文件是否上传完成,返回信息:" + replyMsg + " (" + DateTime.Now.ToString() + ")<br/>";
code = replyMsg.Substring(0, 3);
if (code == "900")
{
break;
}
else
{
Thread.Sleep(30000);
}
}
errMsg += "登录网盘->流程完毕,断开连接!(" + DateTime.Now.ToString() + ")<br/>";
//退出FTP服务器
ftpc.quit();
}
catch(Exception e)
{
return errMsg +"异常信息:"+ e.Message;
}
return "1";
}