1 Serv-U连接MySQL操作FTP文件
1-1 配置用户
/* Ftp服务器操作:
* 已完成功能:上传、下载、重命名、删除、新建目录、判断目录是都存在、获取文件大小
* 若出现bug:远程服务器返回错误: (501) 参数或变量中有语法错误。解决方案:将目录中的"/"修改为"//"
*/
/// <summary>
/// FTP的服务器地址,格式ftp://127.0.0.1:21 进入用户的根目录
/// </summary>
private static string FtpConstr = "ftp://127.0.0.1:21";
/// <summary>
/// FTP服务器的用户名
/// </summary>
private static string FtpUserName = "Test1";
/// <summary>
/// Ftp服务器的密码
/// </summary>
private static string FtpPassword = "Test1";
1-2 上传文件
将本地分文件上传到FTP固定目录上
/// <summary>
/// 上传文件到远程ftp 、断点续传 :成功
/// </summary>
/// <param name="path">本地的文件目录</param>
/// <param name="FileName">文件名称</param>
/// <returns></returns>
public static bool UploadFile(string Path, string FileName)
{
string ErroInfo = "";
FileInfo f = new FileInfo(Path+FileName);
Path = Path.Replace("\\", "/");
//传到ftp目录下的这个目录下
Path = FtpConstr + "/data/uploadFile/" + FileName;
//根据Url创建FtpWebRequest对象
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(Path));
reqFtp.UseBinary = true;
//ftp用户名和密码
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
//在一个命令之后被执行,默认为true(连接不会被关闭)
reqFtp.KeepAlive = false;
//指定执行什么命令
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
//上传文件时通知服务器的大小
reqFtp.ContentLength = f.Length;
//缓冲大小为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//打开一个文件流:读取上传文件
FileStream fs = f.OpenRead();
try
{
//把上传文件写入流
Stream Strm = reqFtp.GetRequestStream();
//每次读取文件流2kb
contentLen = fs.Read(buff, 0, buffLength);
//流内容没有结束
while (contentLen != 0)
{
//把内容从fileStream 写入Upload Stream
Strm.Write(buff, 0, buffLength);
contentLen = fs.Read(buff, 0, buffLength);
}
//关闭两个流
Strm.Close();
fs.Close();
ErroInfo = "完成";
return true;
}
catch (Exception ex)
{
ErroInfo = string.Format("因{0},无法完成上传", ex.Message);
return false;
}
}
将本地分文件上传到FTP目录上
/// <summary>
/// 上传文件到远程ftp、断点续传 :成功
/// </summary>
/// <param name="ftpPath">ftp上的文件路径</param>
/// <param name="Path">本地的文件目录</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool UploadFile(string ftpPath, string Path, string FileName)
{
string ErrorInfo = "";
FileInfo f = new FileInfo(Path+ FileName);
Path = Path.Replace("\\", "/");
//在Ftp上创建目录
bool b = MakeDir(ftpPath);
if (!b)
{
return false;
}
Path = FtpConstr + ftpPath + FileName;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(Path));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.ContentLength = f.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = f.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
ErrorInfo = "完成";
return true;
}
catch (Exception ex)
{
ErrorInfo = string.Format("因{0},无法上传", ex.Message);
return false;
}
}
1-3 下载文件
从FTP 服务器下载文件功能、断点续传 :成功
/// <summary>
/// 从FTP 服务器下载文件功能、断点续传 :成功
/// </summary>
/// <param name="FtpFilePath">FTP下载的地址</param>
/// <param name="FilePath">存放在本地的地址</param>
/// <param name="FileName">保存的文件名称</param>
/// <returns></returns>
public static bool Download(string FtpFilePath, string FilePath, string FileName)
{
try
{
FilePath = FilePath.Replace("我的电脑\\", "");
string onlyFileName = Path.GetFileName(FileName);
// string newFileName = FileName + onlyFileName;
string newFileName = FilePath + FileName;
if (File.Exists(newFileName))
{
File.Delete(newFileName);
}
FtpFilePath = FtpFilePath.Replace("\\", "/");
string URL = FtpConstr + FtpFilePath;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(URL));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
//reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
Stream StreamFtp = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = StreamFtp.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = StreamFtp.Read(buffer, 0, bufferSize);
}
StreamFtp.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception ex)
{
//ErrorInfo=string.Format("因{0},无法下载",ex.Message);
return false;
}
}
1-4 FTP新建文件夹
在FTP上新建文件夹
/// <summary>
/// 新建文件夹:成功
/// </summary>
/// <param name="ftpPath">ftp文件的路径</param>
/// <returns></returns>
public static bool MakeDir(string ftpPath)
{
string ErrorInfo = "";
try
{
//判断Ftp上的文件目录是否存在
bool IsExsis = RemoteFtpDirExists(ftpPath);
if (IsExsis)
{
return true;
}
string url = FtpConstr + ftpPath;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
reqFtp.UseBinary = true;
reqFtp.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFtp.KeepAlive = false;
reqFtp.GetResponse().Close();
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
response.Close();
return true;
}
catch (Exception ex)
{
ErrorInfo = string.Format("因{0},无法下载", ex.Message);
return false;
}
}
1-5 查看FTP上是否存在某文件
查看Ftp上是否存在某文件
/// <summary>
/// 是否存在ftp文件:成功
/// </summary>
/// <param name="Path">ftp路径</param>
/// <returns></returns>
public static bool RemoteFtpDirExists(string FtpPath)
{
bool IsExsis = false;
FtpPath = FtpConstr + FtpPath;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath));
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
reqFtp.KeepAlive = false;
FtpWebResponse resFtp = null;
try
{
resFtp = (FtpWebResponse)reqFtp.GetResponse();
StreamReader stream = new StreamReader(resFtp.GetResponseStream());
//string line = stream.ReadLine();
//while (line != null)
//{
// if (line == FileName)
// {
// IsExsis = true;
// break;
// }
// line = stream.ReadLine();
//}
IsExsis = true;
stream.Close();
resFtp.Close();
return IsExsis;
}
catch (Exception)
{
if (resFtp != null)
{
resFtp.Close();
}
return IsExsis;
}
}
1-6 删除FTP文件
删除FTP文件
/// <summary>
/// 删除:成功
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool Delete(string fileName)
{
try
{
string url = FtpConstr + fileName;
//根据Url创建FtpWebRequest
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
//在一个命令之后被执行,默认为true(连接不会被关闭)
reqFtp.KeepAlive = false;
//执行删除方法
reqFtp.Method = WebRequestMethods.Ftp.DeleteFile;
//Ftp用户名与密码
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
//string sStatus=listResponse.StatusDescription();
response.Close();
return true;
}
catch (Exception ex)
{
//ErrorInfo=string.Format("因{0},无法下载",ex.Message);
return false;
}
}
1-7 获取FTP文件大小
/// <summary>
/// 获得文件大小:成功
/// </summary>
/// <param name="url">FTP文件的完全路径</param>
/// <returns></returns>
public static long GetFileSize(string url)
{
long fileSize = 0;
try
{
url = FtpConstr + url;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
reqFtp.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
fileSize = response.ContentLength;
response.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
return fileSize;
}
1-8 文件重命名
/// <summary>
/// 重命名 :成功
/// </summary>
/// <param name="ftpPath">ftp文件路径</param>
/// <param name="currentFilename"></param>
/// <param name="newFilename">新文件名</param>
public static bool FileRename(string ftpPath, string currentFileName, string newFileName)
{
bool success = false;
try
{
string uri = FtpConstr + ftpPath + currentFileName;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFtp.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
reqFtp.UseBinary = true;
//重命名命令
reqFtp.Method = WebRequestMethods.Ftp.Rename;
//重命名名字
reqFtp.RenameTo = newFileName;
FtpWebResponse ftpWebResponse = (FtpWebResponse)reqFtp.GetResponse();
Stream ftpResponseStream = ftpWebResponse.GetResponseStream();
ftpResponseStream.Close();
ftpWebResponse.Close();
}
catch (Exception)
{
success = false;
}
return success;
}
2、总结
性能:
是否安全:
数据一致性: