码迷,mamicode.com
首页 > Web开发 > 详细

上传文件至ftp

时间:2017-08-22 14:49:20      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:path   log4   ftp   filename   ogg   info   date   net   ilo   

public class UploadFile
    {
        string ftpServerIP;
        string ftpRemotePath;
        string ftpUserID;
        string ftpPassword;
        string ftpURI;
        /// <summary>
        /// 连接FTP
        /// </summary>
        /// <param name="FtpServerIP">FTP连接地址</param>
        /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
        /// <param name="FtpUserID">用户名</param>
        /// <param name="FtpPassword">密码</param>
        public UploadFile(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
        {
            ftpServerIP = FtpServerIP;
            ftpRemotePath = FtpRemotePath;
            ftpUserID = FtpUserID;
            ftpPassword = FtpPassword;
            ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename"></param>
        public void Upload(string filename)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(typeof(UploadFile));
            FileInfo fileInf = new FileInfo(filename);
            string uri = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            try
            {
                using (FileStream fs = fileInf.OpenRead())
                {
                    using (Stream strm = reqFTP.GetRequestStream())
                    {
                        contentLen = fs.Read(buff, 0, buffLength);
                        while (contentLen != 0)
                        {
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);
                        }
                    }
                    if (log.IsErrorEnabled)
                    {
                        log.Error(string.Format("\r\n----------- {0} ----------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                        log.Error(fileInf.FullName + "上传到" + ftpURI + "成功");
                        log.Error("\r\n");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

 

上传文件至ftp

标签:path   log4   ftp   filename   ogg   info   date   net   ilo   

原文地址:http://www.cnblogs.com/z-huan/p/7411472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!