2013-06-29
展开全部
/// <summary>
/// FTP方式上传
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="filename">文件名</param>
/// <param name="ftpServerIP">服务器IP</param>
/// <param name="ftpUserID">用户名</param>
/// <param name="ftpPassword">密码</param>
/// <returns>状态</returns>
public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// 创建由uri提供的FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
try
{
// 提供WebPermission的Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
//buffer的节度设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个IO(System.IO.FileStream)来读取要上传的文件
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//开始用流来上传写入文件
Stream strm = reqFTP.GetRequestStream();
// 从文件流来读取上传数据 一次读2kb(前期可以设置)
contentLen = fs.Read(buff, 0, buffLength);
// 读取完毕时
while (contentLen != 0)
{
// 从文件流中写入内容到FTP上传流
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭文件流和请求流
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
//释放资源
reqFTP.Abort();
Console.WriteLine(ex.Message + ex.StackTrace);
return -2;
}
}
/// FTP方式上传
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="filename">文件名</param>
/// <param name="ftpServerIP">服务器IP</param>
/// <param name="ftpUserID">用户名</param>
/// <param name="ftpPassword">密码</param>
/// <returns>状态</returns>
public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// 创建由uri提供的FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
try
{
// 提供WebPermission的Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
//buffer的节度设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个IO(System.IO.FileStream)来读取要上传的文件
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//开始用流来上传写入文件
Stream strm = reqFTP.GetRequestStream();
// 从文件流来读取上传数据 一次读2kb(前期可以设置)
contentLen = fs.Read(buff, 0, buffLength);
// 读取完毕时
while (contentLen != 0)
{
// 从文件流中写入内容到FTP上传流
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭文件流和请求流
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
//释放资源
reqFTP.Abort();
Console.WriteLine(ex.Message + ex.StackTrace);
return -2;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询