zoukankan      html  css  js  c++  java
  • Ftp 文件上传下载类

    调用:

    FtpOption ftp = new FtpOption("10.128.3.90", "21", "uid", "pwd");
    
    下载文件:
    bool success = ftp.DownLoadNotReName("ST/mylyb/grapes/03/11041112.003", "E:/");
    bool success = ftp.DownLoadReName("ST/myl-yb/grapes/03/11041112.003", "E:/rename.003");
    上传文件:
    bool success=ftp.UploadFile("G:/Ftp.txt","temp"); 

    类:

    using System;
    using System.IO;
    using System.Net;
    namespace WindowsFormsApplication1
    {
        public class FtpOption
        {
            #region

            string serverIP;
            string serverPort;
            string userId;
            string passWord;

            public FtpOption(string serverIP, string serverPort, string userId, string passWord)
            {
                this.serverIP = serverIP;
                this.serverPort = serverPort ?? "21";
                this.userId = userId;
                this.passWord = passWord;
            }

            private FtpWebRequest OpenRequest(Uri uri, string ftpMethord)
            {
                try
                {
                    FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
                    ftpRequest.Credentials = new NetworkCredential(userId, passWord);
                    ftpRequest.Method = ftpMethord;
                    ftpRequest.UseBinary = true;
                    return ftpRequest;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            private FtpWebResponse OpenResponse(Uri uri, string ftpMethord)
            {
                try
                {
                    return this.OpenRequest(uri, ftpMethord).GetResponse() as FtpWebResponse;
                }
                catch
                {
                    throw new Exception("登录到Ftp服务器失败!");
                }
            }

            #endregion

            /// <summary>
            /// 下载(重命名)
            /// </summary>
            /// <param name="sourceFullPath">下载文件全路径</param>
            /// <param name="targetFullPath">下载到本机全路径(包含文件名)</param>
            /// <returns></returns>
            public bool DownLoadReName(string sourceFullPath, string targetFullPath)
            {
                try
                {
                    Uri uri = new Uri(string.Format("ftp://{0}/{1}", serverIP, sourceFullPath));
                    FtpWebResponse downloadResponse = OpenResponse(uri, WebRequestMethods.Ftp.DownloadFile);
                    Stream responseStream = downloadResponse.GetResponseStream();
                    FileStream fileStream = File.Create(targetFullPath);
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while (true)
                    {
                        bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                            break;
                        fileStream.Write(buffer, 0, bytesRead);
                    }
                    fileStream.Close();
                    responseStream.Close();
                    return true;
                }
                catch
                {
                    throw new Exception("获取下载文件失败!");
                }
            }
            /// <summary>
            /// 下载(按原名直接)
            /// </summary>
            /// <param name="loadFullPath">下载文件的全路径</param>
            /// <param name="targePath">下载到指定的地址(E:/)</param>
            /// <returns></returns>
            public bool DownLoadNotReName(string sourceFullPath, string targePath)
            {
                try
                {
                    string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/"));
                    return DownLoadReName(sourceFullPath, targePath + fileName);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            /// <summary>
            /// 文件上传
            /// </summary>
            /// <param name="sourceFullPath">要上传文件的地址(G:/Ftp.txt)</param>
            /// <param name="targetPath">服务器端地址(temp)</param>
            /// <returns></returns>
            public bool UploadFile(string sourceFullPath, string targetPath)
            {
                try
                {
                    string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/") + 1);
                    //检查路径
                    Uri uri = new Uri(string.Format("ftp://{0}:{1}/{2}/{3}", serverIP, serverPort, targetPath, fileName));
                    FtpWebRequest request = this.OpenRequest(uri, WebRequestMethods.Ftp.UploadFile);
                    Stream requestStream = request.GetRequestStream();
                    FileStream fileStream = new System.IO.FileStream(sourceFullPath, FileMode.Open);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while (true)
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                            break;
                        requestStream.Write(buffer, 0, bytesRead);
                    }
                    requestStream.Close();
                    request.GetResponse();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }

    
  • 相关阅读:
    JDBC原理
    练习 map集合被使用是因为具备映射关系 "进度班" "01" "张三" "进度班" "02" "李四" "J1701" "01" "王五" "J1701" "02" "王二" 此信息中,我们要怎样把上述信息装入集合中, 根据班级信息的到所有的所有信
    练习 HashSet 去重复
    集合练习 练习:每一个学生Student都有一个对应的归属地定义为String类型。学生属性:姓名,年龄 注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。 1、描述学生。 2、定义Map容器,将学生作为键,地址作为值存入集合中。 3、获取Map中的元素并进行排序。
    Java学习之Iterator(迭代器)的一般用法 (转)
    int 跟 Integer 的关系
    第十节 集合类Collection和Map
    类 Arrays StringBuilder 跟 StringBuffer 的异同 SimpleDateFormat
    数字转成字母型
    nginx之206异常
  • 原文地址:https://www.cnblogs.com/kingteach/p/2024665.html
Copyright © 2011-2022 走看看