Exemple #1
0
        /// <summary>
        /// 将http转成ftp
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public string ReplaceProtocolHttp2Ftp(string httpUrl)
        {
            string ftpUri;

            if (string.IsNullOrEmpty(httpUrl))
            {
                ftpUri = FtpUrl;
            }
            else if (httpUrl.StartsWith(ReadUrl))
            {
                ftpUri = string.Format("{0}{1}", FtpUrl, httpUrl.Replace(ReadUrl, ""));
            }
            else if (httpUrl.StartsWith(RootPath))
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl.Replace("/" + RootPath, ""), httpUrl);
            }
            else if (httpUrl.StartsWith(string.Format("ftp://{0}", Server)))
            {
                ftpUri = httpUrl;
            }
            else if (httpUrl.StartsWith(FtpUrl))
            {
                ftpUri = httpUrl;
            }
            else if (httpUrl.StartsWith(string.Format("/{0}", RootPath)))
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl.Replace("/" + RootPath, ""), httpUrl);
            }
            else
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl, httpUrl);
            }

            return(ftpUri);
        }
Exemple #2
0
        private WebRequest GetRequest(string path, string method, bool useBinary = false, long contentLength = 0)
        {
            string ftpUri;

            if (!string.IsNullOrEmpty(path) && path.StartsWith("/"))
            {
                path = path.Substring(1);
            }
            if (string.IsNullOrEmpty(path))
            {
                ftpUri = FtpUrl;
            }
            else if (path.StartsWith(RootPath))
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl.Replace("/" + RootPath, ""), path);
            }
            else if (path.StartsWith(string.Format("ftp://{0}", Server)))
            {
                ftpUri = path;
            }
            else
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl, path);
            }
            var ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri));

            ftp.Credentials   = new NetworkCredential(User, Password);
            ftp.Method        = method;
            ftp.UseBinary     = useBinary;
            ftp.ContentLength = contentLength;
            return(ftp);
        }
Exemple #3
0
        public void CreateDirectoryAndUploadFile(byte[] bytes, string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }


            var dir = FtpPathUtil.GetDirectory(filePath);

            if (!IsDirectoryExist(dir))
            {
                CreateDirectory(dir);
            }
            var request = GetRequest(filePath, WebRequestMethods.Ftp.UploadFile, true, bytes.LongLength);
            var strm    = request.GetRequestStream();

            const int buffLength = 2048;


            var chunkCount      = bytes.LongLength / buffLength;
            var chunkLastNumber = bytes.LongLength % buffLength;

            for (int i = 0; i < chunkCount; i++)
            {
                int startIndex = i * buffLength;
                strm.Write(bytes, startIndex, buffLength);
            }
            if (chunkLastNumber > 0)
            {
                strm.Write(bytes, Convert.ToInt32(chunkCount * buffLength), Convert.ToInt32(chunkLastNumber));
            }
            strm.Close();
        }
Exemple #4
0
        public void CreateDirectoryAndUploadFile(Stream stream, string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }


            var dir = FtpPathUtil.GetDirectory(filePath);

            if (!IsDirectoryExist(dir))
            {
                CreateDirectory(dir);
            }
            var       request    = GetRequest(filePath, WebRequestMethods.Ftp.UploadFile, true, stream.Length);
            var       strm       = request.GetRequestStream();
            const int buffLength = 2048;
            var       buff       = new byte[buffLength];

            stream.Seek(0, SeekOrigin.Begin);
            var contentLen = stream.Read(buff, 0, buffLength);

            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = stream.Read(buff, 0, buffLength);
            }
            strm.Close();
        }
Exemple #5
0
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        public void CreateDirectory(string path)
        {
            var    parts = FtpPathUtil.SplitPaths(path);
            string url   = string.Empty;

            parts.ForEach(part =>
            {
                url = FtpPathUtil.Combine(url, part);
                if (!IsDirectoryExist(url))
                {
                    CreateChildDirectory(url);
                }
            });
        }
Exemple #6
0
        private WebResponse SendRequest(string path, string method, bool useBinary = false, long contentLength = 0)
        {
            LoggerFactory.Instance.Info(path);
            string ftpUri;

            if (string.IsNullOrEmpty(path))
            {
                ftpUri = FtpUrl;
            }
            else if (path.StartsWith(ReadUrl))
            {
                ftpUri = string.Format("{0}{1}", FtpUrl, path.Replace(ReadUrl, ""));
            }
            else if (path.StartsWith(RootPath) && !string.IsNullOrEmpty(RootPath))
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl.Replace("/" + RootPath, ""), path);
            }
            else if (path.StartsWith(string.Format("ftp://{0}", Server)))
            {
                ftpUri = path;
            }
            else if (path.StartsWith(FtpUrl))
            {
                ftpUri = path;
            }
            else if (path.StartsWith(string.Format("/{0}", RootPath)))
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl.Replace("/" + RootPath, ""), path);
            }
            else
            {
                ftpUri = FtpPathUtil.Combine(FtpUrl, path);
            }

            LoggerFactory.Instance.Info(ftpUri);
            var ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri));

            ftp.Credentials   = new NetworkCredential(User, Password);
            ftp.Method        = method;
            ftp.UseBinary     = useBinary;
            ftp.ContentLength = contentLength;
            return(ftp.GetResponse());
        }