コード例 #1
0
        /**
         * FTP Server에 데이터를 실제로 전송한다.
         */
        private bool sendData(string ftpPath, string inputFile)
        {
            bool result = false;

            if (inputFile == null || inputFile == "")
            {
                return(false);
            }

            // WebRequest.Create로 Http,Ftp,File Request 객체를 모두 생성할 수 있다.
            FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpPath);

            req.UsePassive = false;
            // FTP 업로드한다는 것을 표시
            req.Method = WebRequestMethods.Ftp.UploadFile;
            // 쓰기 권한이 있는 FTP 사용자 로그인 지정
            req.Credentials = new NetworkCredential(ftpUser, ftpPass);

            //log.Log("[sendData] ftpPath :" + ftpPath + ", inputFile : " + inputFile);

            // 입력파일을 바이트 배열로 읽음
            byte[] data;
            using (StreamReader reader = new StreamReader(inputFile))
            {
                //data = Encoding.UTF8.GetBytes(reader.ReadToEnd());
                data = Encoding.UTF8.GetBytes(reader.ReadToEnd());
            }

            try
            {
                // RequestStream에 데이타를 쓴다
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                }

                // FTP Upload 실행
                using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse())
                {
                    // FTP 결과 상태 출력
                    Console.WriteLine("[ FtpSend ] Upload completed : {0}, {1}", resp.StatusCode, resp.StatusDescription);
                    string logMsg = String.Format("[ FtpSend ] Upload completed : {0}, {1}", resp.StatusCode, resp.StatusDescription);
                    //log.Log(logMsg);
                    result = true;
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("[ FtpSend ] WebException Error : " + ex.ToString());
                log.Log("[ FtpSend ] WebException error : " + ex.ToString());
                result = false;

                FtpWebResponse response = (FtpWebResponse)ex.Response;
                Console.WriteLine("[ FtpSend ] Response : " + response.StatusCode);
                Console.WriteLine("[ FtpSend ] " + response.ToString());
            }

            return(result);
        }
コード例 #2
0
        private void synchroniseTodaysLogfile()
        {
            try
            {
                string fileName = DateTime.Now.ToString("yyyy_MM_dd") + ".log";

                string uri = string.Format("ftp://{0}/Ents/Logs/{1}", _strFTPServer, fileName);

                Console.WriteLine("{0}: Try to synchronize logfile: {1}", DateTime.Now, uri);

                // create the ftp request
                WebRequest request = FtpWebRequest.Create(uri);
                request.Credentials = new NetworkCredential(_strUsername, _strPassword);

                // state that we uploading the file
                request.Method  = WebRequestMethods.Ftp.AppendFile;
                request.Timeout = 600000;
                ((FtpWebRequest)request).KeepAlive = false;
                //                ((FtpWebRequest)request).EnableSsl = true;

                ServicePointManager.ServerCertificateValidationCallback =
                    (s, certificate, chain, sslPolicyErrors) => true;

                using (Stream ftpStream = request.GetRequestStream())
                    using (FileStream fs = File.Open(_rootPath + fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        fs.Seek(_actualFileSize, SeekOrigin.Begin);
                        fs.CopyTo(ftpStream);
                        _actualFileSize = fs.Length;
                    }

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                if (response.StatusCode != FtpStatusCode.ClosingData)
                {
                    Console.WriteLine("Response: {0}, {1}", response.ToString(), response.StatusCode);
                }
                response.Close();
            }
            catch (Exception excp)
            {
                Console.WriteLine("Exception: {0}, {1} while trying to upload file!", excp.Message, excp.StackTrace);
                if (excp.Message.IndexOf("550") != -1)
                {
                    Console.WriteLine("------ > Synchronise all files! <--------");
                    synchroniseAllFiles();
                }
            }
        }