linux ftp下载文件显示进度条,从FTP下载文件带进度条

http://www.cnblogs.com/-040506/p/3501946.html

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 #region 获取FTP服务器上指定文件的大小

2 ///

3 /// 获取FTP服务器上指定文件的大小

4 ///

5 /// FTP服务器上的文件名

6 ///

7 private int GetFtpFileSize(string fileName)

8 {

9 FtpWebRequest reqFTP;

10 int fileSize = 0;

11 try

12 {

13 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://211.149.150.137/feibiht/upLoad/rar/" + fileName));

14

15 reqFTP.Credentials = new NetworkCredential("Administrator", "02887140168");

16

17 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;

18

19 reqFTP.UseBinary = true;

20 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

21 Stream ftpStream = response.GetResponseStream();

22 fileSize = (int)response.ContentLength;

23

24 ftpStream.Close();

25 response.Close();

26 }

27 catch (Exception ex)

28 {

29 MessageBox.Show(ex.Message);

30 }

31 return fileSize;

32 }

33 #endregion

34

35

36 #region FTP下载带进度条

37 ///

38 /// FTP下载带进度条

39 ///

40 /// 下载到客户端的地址

41 /// FTP上对应的文件名

42 ///

43 private bool FtpDownload(string filePath, string fileName)

44 {

45

46 FtpWebRequest reqFTP;

47 try

48 {

49 //打开一个文件流 (System.IO.FileStream) 去读要下载的文件

50 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

51

52 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://211.149.150.137/feibiht/upLoad/rar/" + fileName));

53 // ftp用户名和密码

54 reqFTP.Credentials = new NetworkCredential("Administrator", "02887140168");

55 // 指定执行什么命令

56 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

57 // 指定数据传输类型

58 reqFTP.UseBinary = true;

59

60 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

61

62 Stream ftpStream = response.GetResponseStream();

63 //这里其实娶不到FTP上该文件的大小,应另写方法来获取(WebRequestMethods.Ftp.GetFileSize)

64 //long cl = response.ContentLength;

65 // 缓冲大小设置为2kb

66 int bufferSize = 8192;// 4096;// 2048; //局域网内可以放大点

67

68 int readCount;

69

70 byte[] buffer = new byte[bufferSize];

71

72 readCount = ftpStream.Read(buffer, 0, bufferSize);

73 //FTP上文件的大小

74 int allbye = this.GetFtpFileSize(fileName);

75 int startbye = 0;

76 this.prcBar.Maximum = allbye;

77 this.prcBar.Minimum = 0;

78 this.prcBar.Visible = true;

79 this.label1.Visible = true;

80 // 流内容没有结束

81 while (readCount > 0)

82 {

83 // 把内容从file stream 写入 DownLoadStream

84 outputStream.Write(buffer, 0, readCount);

85

86 readCount = ftpStream.Read(buffer, 0, bufferSize);

87 //

88 startbye += readCount;

89 this.label1.Text = "已下载:" + (int)(startbye / 1024) + "KB/" + "总长度:" + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + fileName;

90 prcBar.Value = startbye;

91 Application.DoEvents();

92 Thread.Sleep(5);

93 }

94 //this.prcBar.Visible = false;

95 this.Visible = false;

96 MessageBox.Show("文件下载成功!!!","提示");

97 //this.label1.Text = "下载成功!";

98 ftpStream.Close();

99 outputStream.Close();

100 response.Close();

101 return true;

102

103 }

104 catch (Exception ex)

105 {

106 MessageBox.Show(ex.Message);

107 return false;

108 }

109 finally

110 {

111 }

112 }

113 #endregion

View Code

转载于:https://www.cnblogs.com/-040506/p/3501946.html

标签:FTP,进度条,int,fileName,ftpStream,reqFTP,response,下载

来源: https://blog.csdn.net/weixin_30393907/article/details/97233662

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值