FTP客户端和服务器的设计与实现
1 毕业设计目的和意义 2
1.1 毕业设计目的 3
1.1.1 目的一:面向系统的软件开发 3
1.1.2 目的二:面向网络应用的软件开发 3
2.毕业设计意义 3
2 毕业设计设计 4
2.1 概述 4
2.2 毕业设计原理 4
2.2.1 使用FTP协议下载文件的流程 4
2.2.2 相关类库说明 6
2.3 毕业设计方案 8
2.3.1 FTP客户端设计 8
2.3.2 FTP服务器端设计 16
结论 26
- 程序主要界面及结果 26
- 程序源程序 32
客户端代码: 32
服务器端代码: 47
参考文献 69
2 毕业设计设计
2.1 概述
FTP协议(文件传输协议)是一种网络数据传输协议,用于文件传输,可以将文件从一台计算机发送到另一台计算机。传输的文件格式多种多样,内容包罗万象,如电子报表、声音、程序和文档文件等。
早期认为的FTP是一个ARPA计算机网络上主机间文件传输的用户级协议。他的主要功能是方便主机间的文件传输,并且允许在其他主机上进行方便的存储和文件处理。
而根据现在的FTP的定义,FTP的主要功能为:
促进文件的共享,包括计算机程序和数据。
支持间接地使用远程计算机。
不会因为各类主机文件存储系统的差异而受影响。
可靠并且有效的传输数据。
FTP可以被用户在终端使用,通常是给程序使用的。FTP中主要采用了TCP协议和Telnet协议。
2.2 毕业设计原理
2.2.1 使用FTP协议下载文件的流程
以一个典型的FTP会话模型为例,如图2-1所示。在本地主机前的用户,希望把文件传送到一台远程主机上或者从这台远程主机上获取下载一些文件,用户需要做的是提供一个登录名和一个登录密码来进行访问。身份认证信息确认后,他就可以在本地文件系统和远程文件系统之间传送文件。如图2-1所示,用户通过一个FTP用户代理与FTP服务器交换信息。用户必须首先提供远程主机的主机名,本文转载自http://www.biyezuopin.vip/onews.asp?id=14921这样才能让本地主机中的FTP客户端进程建立一个与远程主机中的FTP服务器端进程之间的连接通信。紧接着用户提供用户名和密码,这些信息将被确认为FTP参数经由TCP连接传送到服务器上。认证信息得到确认后,该用户就有权在本地文件系统和远程文件系统之间复制文件。
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;namespace Client
{public partial class MainWindow : Form{private LoginWindow parent;Point mouseOff;//用于获取鼠标位置bool leftFlag;//移动标识public MainWindow(LoginWindow parent){this.parent = parent;this.parent.Hide();this.StartPosition = FormStartPosition.CenterScreen;InitializeComponent();if (this.parent.ftpUserID == "test"){this.fileListBox.Items.Add("测试用例1");this.fileListBox.Items.Add("测试用例2");this.fileListBox.Items.Add("测试用例3");}else{this.fileListBox.Items.Clear();}}private void Upload(string filename)//上传方法{FileInfo fileInf = new FileInfo(filename);string uri = "ftp://" + this.parent.ftpServerIP + "/" + fileInf.Name;FtpWebRequest reqFTP;reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.parent.ftpServerIP + "/" + fileInf.Name));reqFTP.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);reqFTP.KeepAlive = false;reqFTP.UsePassive = false;reqFTP.Method = WebRequestMethods.Ftp.UploadFile;reqFTP.UseBinary = true;//指定主动方式reqFTP.UsePassive = false;reqFTP.ContentLength = fileInf.Length;int buffLength = 2048;byte[] buff = new byte[buffLength];int contentLen;//打开一个文件流来读入上传的文件FileStream fs = fileInf.OpenRead();try{Stream strm = reqFTP.GetRequestStream();contentLen = fs.Read(buff, 0, buffLength);while (contentLen != 0){strm.Write(buff, 0, contentLen);contentLen = fs.Read(buff, 0, buffLength);}strm.Close();fs.Close();MessageBox.Show("上传成功!");}catch (Exception ex){MessageBox.Show(ex.Message, "上传出错");}}public void DeleteFTP(string fileName)//删除功能{try{string uri = "ftp://" + this.parent.ftpServerIP + "/" + fileName;FtpWebRequest reqFTP;reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.parent.ftpServerIP + "/" + fileName));reqFTP.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);reqFTP.KeepAlive = false;reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;reqFTP.UsePassive = false;string result = String.Empty;FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();long size = response.ContentLength;Stream datastream = response.GetResponseStream();StreamReader sr = new StreamReader(datastream);result = sr.ReadToEnd();sr.Close();datastream.Close();response.Close();MessageBox.Show("删除成功!");}catch{MessageBox.Show("删除失败,刷新或稍后再试!");}}public string[] GetFileList()//获取文件列表方法{string[] downloadFiles;StringBuilder result = new StringBuilder();FtpWebRequest reqFTP;try{reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.parent.ftpServerIP + "/"));reqFTP.UseBinary = true;reqFTP.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;reqFTP.UsePassive = false;WebResponse response = reqFTP.GetResponse();StreamReader reader = new StreamReader(response.GetResponseStream());string line = reader.ReadLine();while (line != null){result.Append(line);result.Append("\n");line = reader.ReadLine();}result.Remove(result.ToString().LastIndexOf('\n'), 1);reader.Close();response.Close();return result.ToString().Split('\n');}catch (Exception ex){MessageBox.Show(ex.Message);downloadFiles = null;return downloadFiles;}}private string[] GetFilesDatailList(string filename)//获取文件详细信息列表{// if failed ,return null info.string[] ret = null;try{StringBuilder result = new StringBuilder();FtpWebRequest ftp;ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.parent.ftpServerIP + "/"));ftp.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;ftp.UsePassive = false;WebResponse response = ftp.GetResponse();StreamReader reader = new StreamReader(response.GetResponseStream());string line = reader.ReadLine();while (line != null){string[] info = line.Split('\t');if (info[0] == filename){ret = info;break;}else{line = reader.ReadLine();}}return ret;}catch (Exception ex){MessageBox.Show(ex.Message);return ret;}}private void Download(string filePath, string fileName)//下载方法{FtpWebRequest reqFTP;Uri u = new Uri("ftp://" + parent.ftpServerIP + "/" + fileName);try{FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);reqFTP = (FtpWebRequest)FtpWebRequest.Create(u);reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;reqFTP.UseBinary = true;reqFTP.UsePassive = false;reqFTP.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();Stream ftpStream = response.GetResponseStream();long cl = response.ContentLength;int bufferSize = 2048;int readCount;byte[] buffer = new byte[bufferSize];readCount = ftpStream.Read(buffer, 0, bufferSize);while (readCount > 0){outputStream.Write(buffer, 0, readCount);readCount = ftpStream.Read(buffer, 0, bufferSize);}ftpStream.Close();outputStream.Close();response.Close();}catch{}}private void Rename(string currentFilename, string newFilename)//重命名方法{FtpWebRequest reqFTP;try{reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.parent.ftpServerIP + "/" + currentFilename));reqFTP.Method = WebRequestMethods.Ftp.Rename;reqFTP.RenameTo = newFilename;reqFTP.UseBinary = true;reqFTP.UsePassive = false;reqFTP.Credentials = new NetworkCredential(this.parent.ftpUserID, this.parent.ftpPassword);FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();Stream ftpStream = response.GetResponseStream();ftpStream.Close();response.Close();MessageBox.Show("重命名成功!");}catch (Exception ex){MessageBox.Show(ex.Message);}}private void closeBtn_Click(object sender, EventArgs e){this.Close();}private void deleteBtn_Click(object sender, EventArgs e){if (fileListBox.SelectedItem == null){MessageBox.Show("请选择你需要删除的文件!");}else{string fileName = fileListBox.SelectedItem.ToString();DialogResult dr = MessageBox.Show($"确认要删除文件{fileName}吗?", "确认删除",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);if (dr == DialogResult.OK){DeleteFTP(fileName);getFileBtn_Click(null, null);}}}private void getFileBtn_Click(object sender, EventArgs e){string[] filenames = this.GetFileList();fileListBox.Items.Clear();try{foreach (string filename in filenames){fileListBox.Items.Add(filename);}}catch{}}private void uploadBtn_Click(object sender, EventArgs e){OpenFileDialog opFilDIg = new OpenFileDialog();if (opFilDIg.ShowDialog() == DialogResult.OK){Upload(opFilDIg.FileName);getFileBtn_Click(null, null);}}private void detailButton_Click(object sender, EventArgs e){if (fileListBox.SelectedItem == null){MessageBox.Show("请选择文件!");}else{string filename = fileListBox.SelectedItem.ToString();string[] details = GetFilesDatailList(filename);if (details == null){MessageBox.Show("无法获取文件的详细信息!请重试");}else{MessageBox.Show($"Raw response fropm server to {parent.ftpUserID}:\n{details[0]}\n{details[1]}\n{details[2]}\n{details[3]}");}}}private void downloadBtn_Click(object sender, EventArgs e){if (fileListBox.SelectedItem == null){MessageBox.Show("请选择需要下载的文件!");return;}FolderBrowserDialog fldDlg = new FolderBrowserDialog();string fileName = fileListBox.SelectedItem.ToString();if (fileName != string.Empty){if (fldDlg.ShowDialog() == DialogResult.OK){Download(fldDlg.SelectedPath, fileName);}}else{MessageBox.Show("请选择下载的文件!");}}private void backBtn_Click(object sender, EventArgs e){this.parent.Show();this.Hide();}private void renameBtn_Click(object sender, EventArgs e){if (fileListBox.SelectedItem == null){MessageBox.Show("必须选中你想要重命名的文件!");}else{string currentFileName = fileListBox.SelectedItem.ToString();string newFileName = filenameBox.Text.ToString();DialogResult dr = MessageBox.Show($"确认要重命名文件{currentFileName}->{newFileName}吗?", "确认重命名",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);if (dr == DialogResult.OK){if (newFileName.Trim() != ""){Rename(currentFileName, newFileName);getFileBtn_Click(null, null);}else{MessageBox.Show("重命名不能为空!");}}}}private void fileListBox_DrawItem(object sender, DrawItemEventArgs e){Color foreColor;Font font;e.DrawBackground();if ((e.State & DrawItemState.Selected) == DrawItemState.Selected){//如果当前行为选中行。//绘制选中时要显示的蓝色边框。Color c = SystemColors.ControlDark;foreColor = Color.Black; //Color.FromArgb(6, 82, 121);font = new Font("黑体", 11, FontStyle.Bold);e.Graphics.FillRectangle(new SolidBrush(c), e.Bounds);//绘制背景}else{font = e.Font;foreColor = e.ForeColor;}// e.DrawFocusRectangle();StringFormat strFmt = new System.Drawing.StringFormat();strFmt.Alignment = StringAlignment.Center; //文本垂直居中strFmt.LineAlignment = StringAlignment.Center; //文本水平居中e.Graphics.DrawString(fileListBox.Items[e.Index].ToString(), font, new SolidBrush(foreColor), e.Bounds, strFmt);}private void MainWindow_MouseDown(object sender, MouseEventArgs e){mouseOff = new Point(e.X, e.Y);//获取当前鼠标位置leftFlag = true;//用于标记窗体是否能移动(此时鼠标按下如果说用户拖动鼠标则窗体移动)}private void MainWindow_MouseMove(object sender, MouseEventArgs e){if (leftFlag){Location = new Point(Control.MousePosition.X - mouseOff.X, Control.MousePosition.Y - mouseOff.Y);}}private void MainWindow_MouseUp(object sender, MouseEventArgs e){if (leftFlag){leftFlag = false; //释放鼠标标识为false 表示窗体不可移动}}private void MainWindow_FormClosed(object sender, FormClosedEventArgs e){this.parent.Close();}private void fileListBox_MeasureItem(object sender, MeasureItemEventArgs e){e.ItemHeight = 40;}private void MainWindow_VisibleChanged(object sender, EventArgs e){if (this.Visible){this.parent.Hide();}if (this.parent.ftpUserID == "test"){this.InfoLabel.Text = "Test Mode.";}else{this.InfoLabel.Text = $"Welcome! {this.parent.ftpUserID}. ";}}private void closeBtn_MouseEnter(object sender, EventArgs e){this.closeBtn.BackgroundImage = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("close_clicked");}private void closeBtn_MouseLeave(object sender, EventArgs e){this.closeBtn.BackgroundImage = (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject("close");}private void fileListBox_SelectedIndexChanged(object sender, EventArgs e){if (fileListBox.SelectedItem != null){string filename = fileListBox.SelectedItem.ToString();filenameBox.Text = filename;string[] details = GetFilesDatailList(filename);titleLabel.Text = filename;if (details == null){DetailLabel.Text = "无法获取文件的详细信息!请重试";}else{DetailLabel.Text = $"创建日期: {details[1]}\n文件大小: {details[2]}KB\n文件类型: {details[3]}";}}}}
}
FTP客户端和服务器的设计与实现相关推荐
- 【计算机网络】应用层 : FTP 文件传输协议 ( FTP 客户端 和 服务器 | FTP 工作原理 | FTP 传输模式 )
文章目录 一.文件传送协议 二.FTP 客户端 和 服务器 三.FTP 工作原理 四.FTP 传输模式 一.文件传送协议 文件传送协议 : 文件传送协议 FTP ( File Transfer Pro ...
- 从手机用FTP客户端下载服务器中的文件
所需工具: FileZilla Server 下载链接:https://pan.baidu.com/s/122K6Zim9xLJtJp5_pOBqwQ 提取码:bpya 手机软件:AndFTP And ...
- ftp客户端与服务器传文件在哪里,中国大学MOOC: FTP在客户端和服务器端传输文件时,使用的是...
摘要: 仍不任工能胜作的,中国或者工作岗位培训经过调整,合同解除用人劳动单位随时可以,任工能胜者不作劳动.功率也适用于的计算,客户于电不仅流和叠加定律电压的计适用算.服务向正中电极流极源内由负部的不一 ...
- 广工计算机网络课程设计FTP服务器,计算机网络-课程设计报告(FTP客户端的设计和实现).doc...
课程设计报告 课程名称: 计算机网络 设计题目: FTP客户端的设计与实现 系 别: 计算机与信息工程学院 专 业: 计算机科学与技术 组 别: 第一组 起止日期: 2011年11月25 日~ 201 ...
- C#毕业设计——基于C#+asp.net+FTP的FTP客户端设计与实现(毕业论文+程序源码)——FTP客户端
基于C#+asp.net+FTP的FTP客户端设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于C#+asp.net+FTP的FTP客户端设计与实现,文章末尾附有本毕业设计的论文和源码下载地 ...
- FTP客户端设计与实现
互联网的一大特点是实现信息共享,文件传输是信息共享的十分重要的内容之一.随之出现了许多FTP服务器来共享一些信息资源,编写一个操作简单,方便的FTP客户端来下载这些资源受到了人们的极大欢迎. FTP客 ...
- 基于java的ftp客户端_基于Java的FTP客户端软件的设计
基于的FTP客户端软件的设计(含选题审批表,任务书,开题报告,中期检查表,毕业论文8600字,答辩记录) 摘 要:FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中 ...
- windows ftp服务器_ftp客户端软件,推荐6个流行的FTP客户端软件
无论你是做网站工作,还是运行一个家庭FTP服务器,或者你只是喜欢高速下载,一个稳定且功能齐全的FTP客户端工具都可以节省你大量时间和生命,现在有大量的免费或者收费的FTP客户端软件供大家选择,这里总结 ...
- CentOS/用FTP客户端软件连接到服务器
在用ProFTPD构建FTP服务器的时候,为了全力保证文件传输时的安全,我们对FTP服务器进行了尽可能保密.安全的配置.比如TLS,并为服务器建立证书(SSL)等等的手段.这也决定了,在这些条件下,F ...
最新文章
- MyEclipse将Java项目打包成jar文件的三种方法
- 排序算法的python实现
- r shiny app的学习和使用,这个我认为是作为大学生最适合的入门网页开发工具!!!
- 过来人经验!聊聊前端工程师的职业规划
- react学习(10)----react数组定义 从0开始 直接加个0下标空
- WPF纯手工两步打造图片切割工具(二)
- vue开发 - 将方法绑定到window对象,给app端调用
- localStorage存储数组以及取数组方法。
- php广告屏如何同步,户外LED大屏广告如何投放才能更吸引人?
- 【Oracle】ORA-30659: too many locations specified for external table
- c# splitContainer1隐藏panel1/2
- 网易云、酷狗、QQ音乐歌单接口API
- Win10系统下向MS Word2019中添加NoteExpress插件
- 理解为什么女孩子都希望进国企了
- PyTorch实现断点继续训练
- 常用卫星遥感影像数据源
- Go语言panic详解中
- 定积分不等式套路总结
- 变电站智能化改造升级 数字化运营的意义
- 嵌入式开发设备的组成
热门文章
- Cannot download sources Sources not found for
- emWin默认皮肤下重新设置颜色
- 一文读懂P2SH和P2WSH
- Google:host配置
- SAP 财务 发票校验
- C++ 操作承载网络/虚拟网卡
- 2023互联网行业发展趋势前景分析
- vue 使用leaflet绘制平面图(二)
- Redis 个人狂神说视频学习笔记
- board/freescale/mx6ull_alientek_emmc/Kconfig:15: ‘endif‘ in different file than ‘if‘