首页 > Windows开发 > 详细

C#winform ftp上传下载

时间:2021-05-08 22:25:42      阅读:21      评论:0      收藏:0      [点我收藏+]

public class FtpHelper
{
static string ftpServerIP = "ftp://127.0.0.1:2120";
static string ftpUserID = "admin";
static string ftpPassword = "password";
///


/// FTP下载
///

public static void FtpDownload(string baobiaoid)
{
string str = "/Report/" + baobiaoid + ".xml";
string s = "Report/" + baobiaoid + ".xml";
string filename = baobiaoid + ".xml";
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/Report/"));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
string line = reader.ReadToEnd();
var index = line.IndexOf(filename);
if (index == -1)
{
reqFTP = null;
response.Close();
reader.Close();

            FileStream fs1 = new FileStream(s, FileMode.Create, FileAccess.Write);//创建写入文件 
            StreamWriter sw = new StreamWriter(fs1);
            sw.Close();
            fs1.Close();

            XmlDocument doc = new XmlDocument();
            string st = "Report/mb.xml";
            doc.Load(@st);
            string a = doc.OuterXml.ToString();
            string[] ster = a.Split();
            File.AppendAllLines(@s, ster);
        }
        else
        {
            reqFTP = null;
            response.Close();
            reader.Close();
            FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpServerIP + str);
            ftpWebRequest.Credentials = new System.Net.NetworkCredential(ftpUserID, ftpPassword);
            ftpWebRequest.UseBinary = true;
            ftpWebRequest.UsePassive = true;
            ftpWebRequest.KeepAlive = true;

            ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
            Stream ftpStream = ftpWebResponse.GetResponseStream();
            FileStream localFileStream = new FileStream(s, FileMode.Create);
            int bufferSize = 1 * 1024 * 1024;
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            localFileStream.Close();
            ftpStream.Close();
            ftpWebResponse.Close();
            ftpWebRequest = null;
        }
    }
    /// <summary>
    /// FTP上传
    /// </summary>
    /// <param name="strFtpPath"></param>
    /// <param name="strFileName"></param>
    /// <param name="strFtpUser"></param>
    /// <param name="strFtpPassWord"></param>
    /// <param name="strUploadFileName"></param>
    public static void FtpUpload(string baobiaoid)
    {
        string str = "/Report/" + baobiaoid + ".xml";
        string s = "Report/" + baobiaoid + ".xml";
        FileInfo uploadFileInfo = new FileInfo(s);//待上传文件
        FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpServerIP + str);//上传的地址名称

        ftpWebRequest.UseBinary = true;
        ftpWebRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        ftpWebRequest.KeepAlive = false;
        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpWebRequest.ContentLength = uploadFileInfo.Length;

        Stream ftpStream = ftpWebRequest.GetRequestStream();

        FileStream uploadFileReadStream = uploadFileInfo.OpenRead();

        int bufferSize = 1 * 1024 * 1024;

        byte[] byteBuffer = new byte[bufferSize];
        int bytesRead = uploadFileReadStream.Read(byteBuffer, 0, bufferSize);
        try
        {
            while (bytesRead > 0)
            {
                ftpStream.Write(byteBuffer, 0, bytesRead);
                bytesRead = uploadFileReadStream.Read(byteBuffer, 0, bufferSize);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        ftpStream.Close();
        uploadFileReadStream.Close();
        ftpWebRequest = null;
    }
}

C#winform ftp上传下载

原文:https://www.cnblogs.com/yzxcj/p/14745995.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!