首页 技术 正文
技术 2022年11月23日
0 收藏 309 点赞 2,862 浏览 1938 个字

类代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO; namespace ConsoleTest{    class FtpDlder    {        public void download(String url, String localFile)        {            FtpWebRequest remoteFileLenReq; // 此请求是为了获取远程文件长度            FtpWebRequest remoteFileReadReq;// 此请求是为了读取文件            Stream readStream = null;       // 读取流            FileStream writeStream = null;  // 写本地文件流            try            {                writeStream = new FileStream(localFile, FileMode.Append);                long startPosition=writeStream.Length;// 读出本地文件已有长度                // 下面代码目的是取远程文件长度                remoteFileLenReq = (FtpWebRequest)FtpWebRequest.Create(url);                remoteFileLenReq.UseBinary = true;                remoteFileLenReq.ContentOffset = 0;                remoteFileLenReq.Method = WebRequestMethods.Ftp.GetFileSize;                FtpWebResponse rsp = (FtpWebResponse)remoteFileLenReq.GetResponse();                long totalByte = rsp.ContentLength;                rsp.Close();                if (startPosition >= totalByte)                {                    System.Console.WriteLine("本地文件长度" + startPosition + "已经大于等于远程文件长度" + totalByte);                    writeStream.Close();                    return;                }                // 初始化读取远程文件请求                remoteFileReadReq = (FtpWebRequest)FtpWebRequest.Create(url);                remoteFileReadReq.UseBinary = true;                remoteFileReadReq.KeepAlive = false;                remoteFileReadReq.ContentOffset = startPosition;                remoteFileReadReq.Method = WebRequestMethods.Ftp.DownloadFile;                FtpWebResponse response = (FtpWebResponse)remoteFileReadReq.GetResponse();                readStream = response.GetResponseStream();                long downloadedByte = startPosition;                ;                byte[] btArray = new byte[bufferSize];                , btArray.Length);                )                {                    downloadedByte += contentSize;                     / totalByte);                    System.Console.WriteLine("percent=" + percent + "%");                    writeStream.Write(btArray, , contentSize);                    contentSize = readStream.Read(btArray, , btArray.Length);                }                readStream.Close();                writeStream.Close();                response.Close();                return;            }            catch (Exception)            {                return;            }            finally            {                if (readStream != null)                {                    readStream.Close();                }                if (writeStream != null)                {                    writeStream.Close();                }            }        }    }}

使用示例:

        static void Main(string[] args)        {            // Ftp下载测试,无用户身份测试            FtpDlder fd = new FtpDlder();            fd.download("ftp://192.168.0.109/jump.jpg", "c:\\asd\\jump.jpg");        }

以上代码参考了 http://blog.csdn.net/jiankunking/article/details/50017009 的代码,在此向原作者表示感谢。

2017年6月28日16:07:53

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774