| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- namespace Core.StlMes.Client.LgResMgt.Mcms
- {
- public class VSFTPHelper
- {
- #region 字段或属性
- public string IP { get; set; }
- public string UserName { get; set; }
- public string Password { get; set; }
- public string FtpPath { get; set; }
- public bool Passive { get; set; }
- public bool Binary { get; set; }
- #endregion
- #region 构造
- /// <summary>
- /// 构造
- /// </summary>
- public VSFTPHelper( string ftpIp,string uid,string pwd , string path )
- {
- IP = ftpIp;
- Passive = true;
- UserName = uid;
- Password = pwd;
- FtpPath = path;
- Binary = true;
- WriteLog(string.Format("VSFTP参数设定: {0} {1} {2}", IP, UserName, Password));
- }
- #endregion
- #region 创建文件夹
- /// <summary>
- /// 创建一个目录
- /// </summary>
- public void CreateDirectory(string filesUrl)
- {
- try
- {
- Uri uri = new Uri("ftp://" + IP + FtpPath + filesUrl);
- List<string> list = new List<string>();
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create(uri); //
- req.Credentials = new NetworkCredential(UserName, Password);
- req.Method = WebRequestMethods.Ftp.MakeDirectory;
- req.UseBinary = Binary;
- FtpWebResponse res = (FtpWebResponse)req.GetResponse();
- //GetFileList(filesUrl); //假如创建不成功,反复调用。。。
- }
- catch (Exception exp)
- {
- }
- }
- /// <summary>
- /// 获取FTP文件列表 /jldata/ftppicture/
- /// </summary>
- /// <returns></returns>
- public List<String> GetFileList(string filesUrl)
- {
- List<string> list = new List<string>();
- try
- {
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + IP + FtpPath + "/" + filesUrl)); //
- req.Credentials = new NetworkCredential(UserName, Password);
- req.Method = WebRequestMethods.Ftp.ListDirectory;
- req.UseBinary = Binary;
- req.UsePassive = Passive;
- using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
- {
- using (StreamReader sr = new StreamReader(res.GetResponseStream()))
- {
- string s;
- while ((s = sr.ReadLine()) != null)
- {
- list.Add(s);
- }
- }
- }
- //这里的list是文件夹下的文件信息
- return list;
- }
- catch (Exception exp)
- {
- CreateDirectory(filesUrl);
- return list;
- }
- }
- #endregion
- #region 上传文件
- /// <summary>
- /// FTP图片上传
- /// </summary>
- /// <param name="localPath">本地文件夹路径</param>
- public bool FtpUp(string localPath)
- {
- try
- {
- //创建文件夹
- CreateDirectory(DateTime.Now.ToString("yyyy-MM-dd"));
- //上传文件
- UploadFile(localPath);
- //未截图成功的,也上传一个图片
- //File.Copy(strFilePath + "\\image\\null.jpg", strPathImage);
- //UploadFile(strPathImage);
- return true;
- }
- catch (Exception exp)
- {
- WriteLog("FTP图片上传失败!" + exp.Message);
- return false;
- }
- }
- private void UploadFile(string localPath)
- {
- FileInfo fi = new FileInfo(localPath);
- FileStream fs = fi.OpenRead();
- long length = fs.Length;
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + IP + FtpPath + DateTime.Now.ToString("yyyy-MM-dd") + "/" + fi.Name);
- req.Credentials = new NetworkCredential(UserName, Password);
- req.Method = WebRequestMethods.Ftp.UploadFile;
- req.UseBinary = Binary;
- req.ContentLength = length;
- req.Timeout = 10 * 1000;
- Stream stream = req.GetRequestStream();
- int BufferLength = 2048; //2K
- byte[] b = new byte[BufferLength];
- int i;
- while ((i = fs.Read(b, 0, BufferLength)) > 0)
- {
- stream.Write(b, 0, i);
- }
- stream.Close();
- stream.Dispose();
- fs.Close();
- }
- #endregion
- #region 写日志
- private static void WriteLog(string str)
- {
- string m_szRunPath;
- try
- {
- m_szRunPath = System.Environment.CurrentDirectory;
- if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
- {
- System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
- }
- string strDate = System.DateTime.Now.ToString("yyyyMMdd");
- string strPathFile = m_szRunPath + "\\log\\" + strDate;
- if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
- {
- Directory.CreateDirectory(strPathFile);
- }
- System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\FTP_" + strDate + ".log", true);
- tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- tw.WriteLine(str);
- tw.WriteLine("\r\n");
- tw.Close();
- }
- catch { }
- }
- #endregion
- }
- }
|