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

asp.net通过ftp上传文件

时间:2016-04-09 21:51:25      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

 public byte[] FtpDownLoad(string filePath)
        {
            try
            {
                FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer + ftpMapPath + filePath), WebRequestMethods.Ftp.DownloadFile);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                if (response == null)
                {
                    return null;
                }
                Stream retStream = response.GetResponseStream();
                List<byte> contentArray = new List<byte>();
                int bt = retStream.ReadByte();
                while (bt > -1)
                {
                    contentArray.Add((byte)bt);
                    bt = retStream.ReadByte();
                }
                retStream.Dispose();
                return contentArray.ToArray();
            }
            catch (Exception ex)
            {
                LogHelper.LogError("FtpDownLoad失败!", ex);
                return null;
            }
        }
        private FtpWebRequest GetFtpWebRequest(Uri uri, string method)
        {
            FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri);

            ftpClientRequest.Proxy = null;
            ftpClientRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            ftpClientRequest.UseBinary = true;
            //ftpClientRequest.UsePassive = true;
            ftpClientRequest.Method = method;
            //ftpClientRequest.Timeout = -1;

            return ftpClientRequest;
        }
        private bool FtpUploadFile(byte[] fileContent,string fullPath)
        {
            try
            {
                string ftpFileDir = Path.GetDirectoryName(fullPath);
                //这个总要执行
                FtpMakeDirByPath(ftpFileDir.Replace("\\","/"));

                FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer+fullPath), WebRequestMethods.Ftp.UploadFile);
                Stream stream = reqFTP.GetRequestStream();
                stream.Write(fileContent, 0, fileContent.Length);
                stream.Flush();
                stream.Close();
                return true;
            }
            catch(Exception ex)
            {
                LogHelper.LogError("FtpUploadFile失败!", ex);
                return false;
            }            
        }
        /// <summary>
        /// 根据路径创建多级目录
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private bool FtpMakeDirByPath(string dirPath)
        {
            if(string.IsNullOrEmpty(dirPath))
            {
                return false;
            }
            dirPath=dirPath.Replace("\\", "/");
            string[] dirs= dirPath.Split(new char[] { ‘/‘ }, StringSplitOptions.RemoveEmptyEntries);
            if (dirs != null && dirs.Length > 0)
            {
                bool result = false;
                string des = fptServer;
                try
                {
                    for (int i = 0; i < dirs.Length; i++)
                    {
                        result = FtpMakeDir(des, dirs[i]);
                        des = des + "/" + dirs[i];
                    }
                    return true;
                }
                catch(Exception ex)
                {
                    LogHelper.LogError("FtpMakeDirByPath失败!", ex);
                    return false;
                }
                
            }
            return false;
        }
        //创建目录
        private bool FtpMakeDir(string destinationPath,string dirName)
        {
            FtpWebResponse response = null;
            try
            {
                Uri uri = new Uri(destinationPath+"/"+ dirName);
                FtpWebRequest reqFTP = GetFtpWebRequest(uri, WebRequestMethods.Ftp.MakeDirectory);
                response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
                return true;

            }
            catch (Exception ex)
            {
                LogHelper.LogError("FtpMakeDir失败!", ex); 
                return false;
            }
        }        

 

asp.net通过ftp上传文件

标签:

原文地址:http://www.cnblogs.com/AlanWinFun/p/5372722.html

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