예제 #1
0
파일: SFTPHelper.cs 프로젝트: 15831944/test
        public SFTPHelper(SshConnectionInfo connectionInfo)
        {
            m_sshCp = new Sftp(connectionInfo.Host, connectionInfo.User);

            if (connectionInfo.Pass != null)
            {
                m_sshCp.Password = connectionInfo.Pass;
            }

            if (connectionInfo.IdentityFile != null)
            {
                m_sshCp.AddIdentityFile(connectionInfo.IdentityFile);
            }
        }
예제 #2
0
        private void exportXml(ConfigXml configXml, string urlInfo, string fileName)
        {
            //string url = "http://www.enterprise-europe-network.ec.europa.eu/ws/pod/html/PodService.mvc";//?pt=TR&oc=FR&sa=20121101093000000&sb=20121110093000000";
            //string url = "http://een.ec.europa.eu/tools/services/podv5/QueryService.svc/GetProfiles";
            string url = "http://een.ec.europa.eu/tools/services/podv6/QueryService.svc/GetProfiles";

            url           += urlInfo;
            this.ConfigXml = configXml;
            string    strxml  = string.Empty;
            WebClient _client = new WebClient();

            _client.BaseAddress = url;
            _client.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            _client.Headers.Add("Accept-Language", "zh-cn");
            _client.Headers.Add("UA-CPU", "x86");
            _client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
            System.IO.Stream       objStream = _client.OpenRead(url);
            System.IO.StreamReader _read     = new System.IO.StreamReader(objStream, System.Text.Encoding.UTF8);
            strxml = _read.ReadToEnd();


            bool   isEmpty       = false;
            string fileNameEmpty = string.Empty;

            fileNameEmpty = fileName + "_References";

            if (strxml.Substring(0, 5) != "<pod>")
            {
                isEmpty = true;
            }
            else
            {
                fileName += "_Profiles_00000-00000";
            }
            string strPath = configXml.getXmlValueById("path");

            int countProfile = 0; //含reference的个数

            if (!isEmpty)
            {                                                                                                                                                                                                                                                                                                                                                                                    //有数据需要保存的文件
                countProfile = Regex.Matches(strxml, "</profile>").Count;
                strxml       = strxml.Replace("<profile xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">", "<profile xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.enterprise-europe-network.ec.europa.eu/schema/pod/pod-profiles-definition/2012/03/21\">"); //替换节点新程序无xmlns="" add by jerry 2014-09-18
                string xmlPath = strPath + "/" + fileName + ".xml";
                if (File.Exists(xmlPath))
                {
                    File.Delete(xmlPath);
                }
                File.WriteAllText(xmlPath, strxml);//保存到本地
            }

            StringBuilder strProfile = new StringBuilder(); //存放reference字符串

            for (int i = 0; i < countProfile; i++)
            {
                strProfile.AppendLine("<reference>");
                strProfile.AppendLine("</reference>");
            }

            //此处是有数据和没有数据都需要保存的空文件
            string xmlPathEmpty = strPath + "/" + fileNameEmpty + ".xml";
            string strEmptyXml  = "<GetReferencesResult xmlns:a=\"http://www.enterprise-europe-network.ec.europa.eu/schema/pod/pod-profiles-query/2011/04/22\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.enterprise-europe-network.ec.europa.eu/services/pod-profile/2011/04/22\">";

            strEmptyXml += strProfile.ToString() + "</GetReferencesResult>";
            if (File.Exists(xmlPathEmpty))
            {
                File.Delete(xmlPathEmpty);
            }
            File.WriteAllText(xmlPathEmpty, strEmptyXml);//保存到本地


            #region SFTP方法

            SshConnectionInfo objInfo = new SshConnectionInfo();
            objInfo.User         = "******";
            objInfo.Host         = "host";
            objInfo.IdentityFile = "key"; //有2中认证,一种基于PrivateKey,一种基于password
            //objInfo.Pass = "******"; 基于密码
            SFTPHelper objSFTPHelper = new SFTPHelper(objInfo);
            objSFTPHelper.Upload("localFile", "remoteFile");              //上传文件
            objSFTPHelper.Download("remoteFile", "localFile");            //下载文件
            ArrayList fileList = objSFTPHelper.GetFileList("remotePath"); //遍历远程文件夹

            #endregion
        }