示例#1
0
        /// <summary>
        /// 判断remotePath是否是远程FTP目录
        /// </summary>
        /// <param name="ftpInfo">远程FTP信息</param>
        /// <param name="remotePath">目录名</param>
        /// <returns></returns>
        public static bool MakeDirectory(FtpInfo ftpInfo, string remotePath)
        {
            try
            {
                //1.删除最后面的目录分隔符
                string path1 = string.Format("{0}/{1}", ftpInfo.RemoteDir, remotePath);
                string uri   = string.Format("ftp://{0}:{1}{2}"
                                             , ftpInfo.ServerIP
                                             , ftpInfo.Port
                                             , PathUtil.DeleteSeparator(path1));

                //2.设置访问FTP参数
                FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                reqFTP.UseBinary   = ftpInfo.IsUseBinary;
                reqFTP.UsePassive  = ftpInfo.FireMode;
                reqFTP.Credentials = new NetworkCredential(ftpInfo.User, ftpInfo.Password);
                reqFTP.Method      = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.KeepAlive   = false;

                //3.读取FTP信息
                using (WebResponse response = reqFTP.GetResponse())
                {
                    response.Close();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="ftpInfo">上传FTP信息</param>
        /// <param name="fileName">上传文件名</param>
        /// <param name="data">文件内容</param>
        /// <param name="errorInfo"></param>
        /// <returns></returns>
        public static bool Upload(FtpInfo ftpInfo, string fileName, byte[] data, ref string errorInfo)
        {
            Stream        requestStream = null;
            FtpWebRequest request;
            string        uri = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(ftpInfo.RemoteDir))
                {
                    uri = string.Format("ftp://{0}:{1}/{2}"
                                        , ftpInfo.ServerIP
                                        , ftpInfo.Port
                                        , Path.GetFileName(fileName)
                                        );
                }
                else
                {
                    uri = string.Format("ftp://{0}:{1}{2}/{3}"
                                        , ftpInfo.ServerIP
                                        , ftpInfo.Port
                                        , PathUtil.DeleteSeparator(ftpInfo.RemoteDir)
                                        , Path.GetFileName(fileName)
                                        );
                }
                request             = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                request.Method      = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpInfo.User, ftpInfo.Password);
                request.KeepAlive   = false;
                request.UseBinary   = ftpInfo.IsUseBinary;
                request.Timeout     = ftpInfo.TimeOut;
                request.UsePassive  = ftpInfo.FireMode;//被动方式连接到FTP服务器

                requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                return(true);
            }
            catch (Exception ex)
            {
                errorInfo = ex.Message;
                return(false);
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }
        }
示例#3
0
        public FtpInfo Clone()
        {
            FtpInfo ftpInfo = new FtpInfo();

            ftpInfo.User        = User;
            ftpInfo.Password    = Password;
            ftpInfo.FireMode    = FireMode;
            ftpInfo.IsUseBinary = IsUseBinary;
            ftpInfo.Port        = Port;
            ftpInfo.RemoteDir   = RemoteDir;
            ftpInfo.ServerIP    = ServerIP;
            ftpInfo.TimeOut     = TimeOut;
            return(ftpInfo);
        }
示例#4
0
        /// <summary>
        /// 检查远程目录是否存在,如果不存在则创建远程FTP目录
        /// </summary>
        /// <param name="ftpInfo">远程FTP信息</param>
        /// <param name="remotePath">目录名</param>
        /// <returns></returns>
        public static bool CheckMakeDirectory(FtpInfo ftpInfo, string remotePath)
        {
            string remoteDir = string.Format("{0}/{1}"
                                             , ftpInfo.RemoteDir
                                             , remotePath);

            try
            {
                //1.获得目录名称
                List <string> remoteDirNames = PathUtil.GetDirectoryName(remoteDir);

                //2.判断远程FTP是否存在目录,如果不存在则创建此目录
                FtpInfo tempFtpInfo = ftpInfo.Clone();
                tempFtpInfo.RemoteDir = "";
                foreach (string dirName in remoteDirNames)
                {
                    //2.1 判断远程FTP是否存在目录
                    tempFtpInfo.RemoteDir = tempFtpInfo.RemoteDir + "/" + dirName;
                    if (CheckDirectoryExists(tempFtpInfo, string.Empty))
                    {
                        continue;
                    }

                    //2.2 如果不存在则创建此目录
                    if (!MakeDirectory(tempFtpInfo, string.Empty))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
示例#5
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="ftpInfo">上传FTP信息</param>
        /// <param name="fileName">上传文件名</param>
        /// <param name="error">返回出错信息</param>
        /// <returns></returns>
        public static bool Upload(FtpInfo ftpInfo, string fileName, ref string error)
        {
            Stream        requestStream = null;
            FtpWebRequest request;
            string        uri = string.Empty;

            fileName = ftpInfo.LocalPath + fileName;

            try
            {
                if (string.IsNullOrEmpty(ftpInfo.RemoteDir))
                {
                    uri = string.Format("ftp://{0}:{1}/{2}"
                                        , ftpInfo.ServerIP
                                        , ftpInfo.Port
                                        , Path.GetFileName(fileName)
                                        );
                }
                else
                {
                    uri = string.Format("ftp://{0}:{1}{2}/{3}"
                                        , ftpInfo.ServerIP
                                        , ftpInfo.Port
                                        , PathUtil.DeleteSeparator(ftpInfo.RemoteDir)
                                        , Path.GetFileName(fileName)
                                        );
                }

                request             = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                request.Method      = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpInfo.User, ftpInfo.Password);
                request.KeepAlive   = false;
                request.UseBinary   = ftpInfo.IsUseBinary;
                request.Timeout     = ftpInfo.TimeOut;
                request.UsePassive  = ftpInfo.FireMode;//被动方式连接到FTP服务器

                const int bufferLength = 2048;
                requestStream = request.GetRequestStream();
                // Get the event to wait on.
                byte[] buffer = new byte[bufferLength];
                using (FileStream stream = File.OpenRead(fileName))
                {
                    int count = 0;
                    int readBytes;
                    do
                    {
                        readBytes = stream.Read(buffer, 0, bufferLength);
                        requestStream.Write(buffer, 0, readBytes);
                        count += readBytes;
                    }while (readBytes != 0);
                }
                return(true);
            }
            catch (Exception ex)
            {
                error = string.Format("上传文件[{0}]到[{1}]出现异常:{2}", fileName, uri, ex.Message);
                return(false);
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }
        }
示例#6
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="ftpInfo"></param>
        /// <param name="fileName"></param>
        /// <param name="path"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool Download(FtpInfo ftpInfo, string fileName, string path, ref string error)
        {
            Stream         ftpStream = null;
            FtpWebRequest  reqFTP;
            FileStream     outputStream = null;
            FtpWebResponse response     = null;
            string         uri          = string.Empty;

            try
            {
                uri = string.Format("ftp://{0}:{1}{2}/{3}"
                                    , ftpInfo.ServerIP
                                    , ftpInfo.Port
                                    , PathUtil.DeleteSeparator(ftpInfo.RemoteDir)
                                    , fileName);

                outputStream       = new FileStream(path, FileMode.Create);
                reqFTP             = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                reqFTP.Method      = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary   = ftpInfo.IsUseBinary;
                reqFTP.Credentials = new NetworkCredential(ftpInfo.User, ftpInfo.Password);
                //被动模式(将 UsePassive 属性设置为 true 可向服务器发送“PASV”命令。此命令请求服务器侦听数据端口并等待连接,而不是在收到传输命令时启动连接。)
                reqFTP.UsePassive = ftpInfo.FireMode;
                response          = (FtpWebResponse)reqFTP.GetResponse();
                ftpStream         = response.GetResponseStream();
                const int bufferSize = 2048;

                byte[] buffer    = new byte[bufferSize];
                int    readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);

                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                error = string.Format("下载文件FTP上文件[{0}]到[{1}]出现异常:{2}", uri, path, ex.Message);
                return(false);
            }
            finally
            {
                if (ftpStream != null)
                {
                    ftpStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }