コード例 #1
0
 /// <summary>  
 /// SFTP获取文件  
 /// </summary>
 /// <param name="path">远程路径</param>  
 /// <param name="remotePath">文件集合</param>  
 /// <param name="localPath">本地路径</param>  
 public void Get(string path, List <object> remotePath, string localPath)
 {
     try
     {
         Connect();
         foreach (string item in remotePath)
         {
             var byt = sftp.ReadAllBytes(PathHelper.MergeUrl(path, item));
             Task.Run(() => { File.WriteAllBytes(PathHelper.MergeUrl(localPath, item), byt); });
         }
         Disconnect();
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));
     }
 }
コード例 #2
0
        ///// <summary>
        ///// 新建目录
        ///// </summary>
        ///// <param name="ftpIp">FTP服务器地址</param>
        ///// <param name="ftpPath">目录名</param>
        ///// <param name="ftpUser">FTP服务器用户名</param>
        ///// <param name="ftpPassword">FTP服务器用户密码</param>
        public static void MakeDir(string ftpIp, string ftpPath, string ftpUser, string ftpPassword)
        {
            string fullDir = FtpParseDirectory(PathHelper.MergeUrl(ftpPath, String.Empty));

            string[] dirs   = fullDir.Split('/');
            string   curDir = "/";

            foreach (var dir in dirs)
            {
                //如果是以/开始的路径,第一个为空
                if (dir != null && dir.Length > 0)
                {
                    try
                    {
                        curDir += dir + "/";
                        FtpMakeDir(ftpIp, curDir, ftpUser, ftpPassword);
                    }
                    catch (Exception)
                    { }
                }
            }
        }
コード例 #3
0
        public static byte[] DownLoadFile(string ftpPath, string fileName, string ftpUser, string ftpPassword, string savePath = "")
        {
            try
            {
                FileStream outputStream = null;
                if (!string.IsNullOrEmpty(savePath))
                {
                    outputStream = new FileStream(savePath, FileMode.Create);
                }

                FtpWebRequest reqFTP;
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(PathHelper.MergeUrl(ftpPath, fileName)));
                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                reqFTP.Method      = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary   = true;
                FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream  = response.GetResponseStream();
                long           cl         = response.ContentLength;
                int            bufferSize = 2048;
                int            readCount;
                byte[]         buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream?.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream?.Close();
                response.Close();
                return(buffer);
            }
            catch (Exception ex)
            {
                Logger.LogInfo($"DownLoadFile Error {ex.Message}");
                throw new Exception(ex.Message);
            }
        }
コード例 #4
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filestream">文件流</param>
        /// <param name="filename">文件名</param>
        /// <param name="ftpPath">文件路径</param>
        /// <param name="ftpUser">FTP服务器用户名</param>
        /// <param name="ftpPassword">FTP服务器用户密码</param>
        public static void UpLoadFile(byte[] filestream, string filename, string ftpPath, string ftpUser, string ftpPassword)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename is empty");
            }

            if (ftpUser == null)
            {
                ftpUser = "";
            }
            if (ftpPassword == null)
            {
                ftpPassword = "";
            }

            //if (!File.Exists(localFile))
            //{
            //    //MyLog.ShowMessage("文件:“" + localFile + "” 不存在!");
            //    return;
            //}

            FtpWebRequest ftpWebRequest   = null;
            MemoryStream  localFileStream = null;
            Stream        requestStream   = null;

            try
            {
                ftpWebRequest               = (FtpWebRequest)FtpWebRequest.Create(PathHelper.MergeUrl(ftpPath, filename));
                ftpWebRequest.Credentials   = new NetworkCredential(ftpUser, ftpPassword);
                ftpWebRequest.UseBinary     = true;
                ftpWebRequest.KeepAlive     = false;
                ftpWebRequest.Method        = WebRequestMethods.Ftp.UploadFile;
                ftpWebRequest.ContentLength = filestream.Length;
                int    buffLength = 4096;
                byte[] buff       = new byte[buffLength];
                int    contentLen;
                //localFileStream = new FileInfo(localFile).OpenRead();
                localFileStream = new MemoryStream(filestream);
                requestStream   = ftpWebRequest.GetRequestStream();
                contentLen      = localFileStream.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    requestStream.Write(buff, 0, contentLen);
                    contentLen = localFileStream.Read(buff, 0, buffLength);
                }
            }
            catch (Exception ex)
            {
                Logger.LogInfo($"ftp upload failed :{ex}");
                throw ex;
                //MyLog.ShowMessage(ex.Message, "FileUpLoad0001");
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
                if (localFileStream != null)
                {
                    localFileStream.Close();
                }
            }
        }