| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Web;
- /// <summary>
- /// UPloadFile 的摘要说明
- /// </summary>
- public class UPloadFile
- {
- public string ftpPath;
- public string ftpdir;
- public string ftpUserID;
- public string ftpPassword;
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="fileinfo">需要上传的文件</param>
- public string UploadLocalToFtp(string localFile)
- {
- string res = "";
- if (!File.Exists(localFile))
- {
- res = "文件:“" + localFile + "” 不存在!";
- return res;
- }
- FileInfo fileInf = new FileInfo(localFile);
- //改后文件
- #region 改后文件
- //string temp_filename = localFile.Split('\\').Last().Split('.')[0] + "_temp";
- //string localfileAft = Path.GetDirectoryName(localFile) + "\\" + temp_filename + ".wav";
- //FileInfo fileInfAft = new FileInfo(localfileAft);
- #endregion
- string ftpURI = "ftp://" + ftpPath + "/";
- if (!string.IsNullOrEmpty(ftpdir))
- ftpURI = ftpURI + ftpdir + "/";
- string uri = ftpURI + fileInf.Name;
- //string uriAft = ftpURI + fileInfAft.Name;
- FtpWebRequest reqFTP;
- //20180209
- //System.GC.Collect();
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象
- reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
- reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
- reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
- reqFTP.UseBinary = true;// 指定数据传输类型
- reqFTP.UsePassive = true;
- reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
- int buffLength = 2048;// 缓冲大小设置为2kb
- byte[] buff = new byte[buffLength];
- int contentLen;
- // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
- //FileStream fs = fileInf.OpenRead();
- using (FileStream fs = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- try
- {
- res += uri;// + localFile;
- using (Stream strm = reqFTP.GetRequestStream())// 把上传的文件写入流
- {
- //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
- contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
- while (contentLen != 0)// 流内容没有结束
- {
- // 把内容从file stream 写入 upload stream
- strm.Write(buff, 0, contentLen);
- contentLen = fs.Read(buff, 0, buffLength);
- }
- //StringBuilder sb = new StringBuilder();
- //while (!strm.EndOfStream)
- //{
- // sb.AppendLine(strm.ReadLine() + "<br>");
- //}
- }
- // 关闭两个流
- //strm.Close();
- fs.Close();
- res = "上传成功!";
- }
- catch (Exception ex)
- {
- res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
- }
- }
- //FtpWebRequest reqFTP1;
- ////20180209
- ////System.GC.Collect();
- //reqFTP1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(uriAft));// 根据uri创建FtpWebRequest对象
- //reqFTP1.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
- //reqFTP1.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
- //reqFTP1.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
- //reqFTP1.UseBinary = true;// 指定数据传输类型
- //reqFTP1.UsePassive = true;
- //reqFTP1.ContentLength = fileInfAft.Length;// 上传文件时通知服务器文件的大小
- //int buffLengthaft = 2048;// 缓冲大小设置为2kb
- //byte[] buffaft = new byte[buffLengthaft];
- //int contentLenaft;
- // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
- //FileStream fs = fileInf.OpenRead();
- //using (FileStream fs1 = new FileStream(localfileAft, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- //{
- // try
- // {
- // res += uriAft + localfileAft;
- // using (Stream strm1 = reqFTP1.GetRequestStream())// 把上传的文件写入流
- // {
- // //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
- // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);// 每次读文件流的2kb
- // while (contentLenaft != 0)// 流内容没有结束
- // {
- // // 把内容从file stream 写入 upload stream
- // strm1.Write(buffaft, 0, contentLenaft);
- // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);
- // }
- // //StringBuilder sb = new StringBuilder();
- // //while (!strm.EndOfStream)
- // //{
- // // sb.AppendLine(strm.ReadLine() + "<br>");
- // //}
- // }
- // // 关闭两个流
- // //strm.Close();
- // fs1.Close();
- // res = "上传成功!";
- // }
- // catch (Exception ex)
- // {
- // res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
- // }
- //}
- return res;
- }
- /// <summary>
- /// 判断ftp服务器上该目录是否存在
- /// </summary>
- /// <param name="ftpPath">FTP路径目录</param>
- /// <param name="dirName">目录上的文件夹名称</param>
- /// <returns></returns>
- private bool CheckDirectoryExist(string ftpPath, string dirName)
- {
- bool flag = true;
- try
- {
- //实例化FTP连接
- FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
- ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
- ftp.Method = WebRequestMethods.Ftp.ListDirectory;
- FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
- response.Close();
- }
- catch (Exception)
- {
- flag = false;
- }
- return flag;
- }
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <param name="ftpPath">FTP路径</param>
- /// <param name="dirName">创建文件夹名称</param>
- public void MakeDir(string ftpPath, string dirName)
- {
- FtpWebRequest reqFTP;
- try
- {
- string ui = (ftpPath + dirName).Trim();
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
- reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
- reqFTP.UseBinary = true;
- reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- ftpStream.Close();
- response.Close();
- //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
- }
- catch (Exception ex)
- {
- //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
- }
- }
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="serverName">服务器文件名称</param>
- /// <param name="localName">需要保存在本地的文件名称</param>
- /// <returns>下载成功返回 true</returns>
- //public bool Download(string serverName, string localName)
- //{
- // bool result = false;
- // using (FileStream fs = new FileStream(localName, FileMode.OpenOrCreate))
- // {
- // try
- // {
- // string url = UrlCombine(Host, RemotePath, serverName);
- // Console.WriteLine(url);
- // FtpWebRequest request = CreateConnection(url, WebRequestMethods.Ftp.DownloadFile);
- // request.ContentOffset = fs.Length;
- // using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
- // {
- // fs.Position = fs.Length;
- // byte[] buffer = new byte[1024 * 4];
- // int count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
- // while (count > 0)
- // {
- // fs.Write(buffer, 0, count);
- // count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
- // }
- // response.GetResponseStream().Close();
- // }
- // result = true;
- // }
- // catch (WebException ex)
- // {
- // // 处理ftp连接中的异常
- // }
- // }
- // return result;
- //}
- }
|