博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ftp 文件上传下载类
阅读量:5265 次
发布时间:2019-06-14

本文共 3490 字,大约阅读时间需要 11 分钟。

调用:

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;
            }
        }
    }
}



转载于:https://www.cnblogs.com/kingteach/archive/2011/04/22/2024665.html

你可能感兴趣的文章
Typescript编译设置
查看>>
批量删除垃圾帖
查看>>
三目运算符
查看>>
js 判断当前是什么浏览器
查看>>
关于购物车添加按钮的动画(vue.js)
查看>>
JAVA环境安装配置
查看>>
js瀑布流 原理实现揭秘 javascript 原生实现
查看>>
史上最浅显易懂的Git教程1
查看>>
【转】机器学习加快设计周期
查看>>
jQuery中运用正则表达式验证输入是否有特殊字符
查看>>
GoFramework框架简介(七)--wrapper篇
查看>>
group_concat
查看>>
apache自带压力测试工具ab详解
查看>>
oracle 笔记
查看>>
【转】implicit declaration of function 这种警告问题的原因及解决方法
查看>>
Android多线程
查看>>
1133. Splitting A Linked List (25)
查看>>
STM32F1新建工程
查看>>
今天被Prism里面的一段代码雷到了
查看>>
cin用法介绍
查看>>