/// <summary>
/// 判断FTP文件是否存在
internal static bool DirectoryExist(FTPparama parama)
{
var ftppath = parama.FTPPath;
if (parama.LX == 1)
{
ftppath = parama.FTPPath + "/" + parama.TXM;
}
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(parama.username, parama.password);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse resFtp = null;
try
{
resFtp = (FtpWebResponse)reqFtp.GetResponse();
FtpStatusCode code = resFtp.StatusCode;//OpeningData
resFtp.Close();
return true;
}
catch
{
if (resFtp != null)
{
resFtp.Close();
}
return false;
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="dirName"></param>
internal static void MakeDir(FTPparama parama)
{
try
{
string uri = parama.FTPPath + "/" + parama.TXM;
//string uri = "ftp://" + ftpServerIP + "/" + dirName;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(parama.username, parama.password);
//Connect(uri);//连接
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
throw new Exception("创建文件失败,原因: " + ex.Message);
}
}
// 上传文件到远程ftp
internal static bool UploadFile(FTPparama parama)
{
string ftppath = parama.FTPPath + "/" + parama.TXM;
string pathfile = parama.path + @"\" + parama.filname;
string erroinfo = "";
string path;
FileInfo f = new FileInfo(pathfile);
path = parama.path.Replace("\\", "/");
//bool b = MakeDir(ftpPath);
//if (b == false)
//{
// return false;
//}
path = ftppath + "/" + parama.filname;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(parama.username, parama.password);
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();
erroinfo = "完成";
return true;
}
catch (Exception ex)
{
erroinfo = string.Format("因{0},无法完成上传", ex.Message);
return false;
}
}
/// <summary>
/// 从ftp服务器上获得文件列表
/// </summary>
/// <returns></returns>
internal static List<string> GetFile(FTPparama parama)
{
List<string> strs = new List<string>();
try
{
string uri = parama.FTPPath + "/" + parama.TXM; //目标路径 path为服务器地址
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(parama.username, parama.password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
string line = reader.ReadLine();
while (line != null)
{
if (!line.Contains("<DIR>"))
{
string msg = line.Substring(39).Trim();
strs.Add(msg);
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
return strs;
}
catch (Exception ex)
{
Console.WriteLine("获取文件出错:" + ex.Message);
}
return strs;
}
/// <summary>
/// 从ftp服务器删除文件的功能
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
internal static bool DeleteFile(FTPparama parama)
{
try
{
string url = parama.path + parama.filname;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.DeleteFile;
reqFtp.Credentials = new NetworkCredential(parama.username, parama.password);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
response.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 从ftp服务器下载文件的功能
/// </summary>
/// <param name="ftpfilepath">ftp下载的地址</param>
/// <param name="filePath">存放到本地的路径</param>
/// <param name="fileName">保存的文件名称</param>
/// <returns></returns>
internal static bool Download(FTPparama parama)
{
string paths = "";
try
{
paths = parama.path.Replace("\\", "/");
String onlyFileName = Path.GetFileName(parama.filname);
string newFileName = parama.path + @"/" + onlyFileName;
if (File.Exists(newFileName))
{
File.Delete(newFileName);
}
parama.FTPPath = parama.FTPPath.Replace("\\", "/");
string url = parama.FTPPath + "/" + parama.TXM + "/" + parama.filname; ;
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(parama.username, parama.password);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception ex)
{
//errorinfo = string.Format("因{0},无法下载", ex.Message);
return false;
}
}
/// <summary>
/// 从ftp服务器上获得文件夹列表
/// </summary>
/// <param name="RequedstPath">服务器下的相对路径</param>
/// <returns></returns>
internal static List<string> GetDirctory(FTPparama parama)
{
List<string> strs = new List<string>();
try
{
string uri = parama.FTPPath; //目标路径 path为服务器地址
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(parama.username, parama.password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
string line = reader.ReadLine();
while (line != null)
{
if (line.Contains("<DIR>"))
{
string msg = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
strs.Add(msg);
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
return strs;
}
catch (Exception ex)
{
Console.WriteLine("获取目录出错:" + ex.Message);
}
return strs;
}
}
class FTPparama
{
public string FTPPath { get; set; }//ftp服务器地址
public string path { get; set; }//本地路径
public string username { get; set; }//ftp文件访问的用户名
public string password { get; set; }//ftp文件访问的密码
public string filname { get; set; }//文件名
public string TXM { get; set; }//条形码
public int LX { get; set; }//判断是什么时候检验文件夹是否存在的,等于1的时候路径加了条形码
}
[更多使用,参考](https://www.cnblogs.com/zhenzaizai/p/7434669.html)
FTP文件上传、下载、获取文件列表、文件夹列表
最新推荐文章于 2024-05-03 04:16:46 发布