123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Base.CommTool
- {
- /// <summary>
- /// FPT帮助类
- /// </summary>
- public class FTPWebsiteHelper
- {
- /// <summary>
- /// FTP地址 地址 +路径
- /// </summary>
- public string ftpPath { get; set; }
- /// <summary>
- /// FTP账号
- /// </summary>
- public string ftpUserID { get; set; }
- /// <summary>
- /// FTP密码
- /// </summary>
- public string ftpPassword { get; set; }
- /// <summary>
- /// FTP密码
- /// </summary>
- public string ftpStr { get; set; }
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <param name="localFile">要上传到FTP服务器的本地文件</param>
- /// <param name="ftpPath">FTP地址</param>
- public void CreateDictionary(string dateStr)
- {
- try
- {
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath + "/" + dateStr);// 根据uri创建FtpWebRequest对象
- reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
- reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
- reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;// 指定执行什么命令
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- ftpStream.Close();
- response.Close();
- }
- catch (Exception ex)
- {
- }
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="localFile">要上传到FTP服务器的本地文件</param>
- /// <param name="ftpPath">FTP地址</param>
- public void UpLoadFile(string localFile)
- {
- if (!File.Exists(localFile))
- {
- return;
- }
- FileInfo fileInf = new FileInfo(localFile);
- FtpWebRequest reqFTP;
- string dateStr = DateTime.Now.ToString("yyyyMMdd");
- this.ftpStr = dateStr;
- CreateDictionary(dateStr);
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath + "/" + dateStr + "/" + fileInf.Name);// 根据uri创建FtpWebRequest对象
- reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
- reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
- reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
- reqFTP.UseBinary = true;// 指定数据传输类型
- reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
- int buffLength = 2048;// 缓冲大小设置为2kb
- byte[] buff = new byte[buffLength];
- int contentLen;
- // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
- FileStream fs = fileInf.OpenRead();
- try
- {
- Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
- 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);
- }
- // 关闭两个流
- strm.Close();
- fs.Close();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
|