FTP客户端和服务器的设计与实现

article/2025/9/18 19:19:01

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

  1. 程序主要界面及结果 26
  2. 程序源程序 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]}";}}}}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


http://chatgpt.dhexx.cn/article/VdCiDLSq.shtml

相关文章

移动端适配方案总结

目录 一、背景介绍1.1 为什么要进行移动端适配1.2 移动端适配方案 二、rem方案2.1 什么是rem2.2 怎么根据屏幕尺寸设置根元素html的font-size2.3 postcss-pxtorem 三、viewport方案3.1 什么是viewport方案3.2 postcss-px-to-viewport 四、总结(如果只想看实现步骤可…

移动端适配的几种方式

百分比适配方式 这种方法&#xff0c;只是宽度能适配&#xff0c;高度不能适配&#xff0c;只能设置某个高度固定死 需求&#xff1a;是四个div高度为100px&#xff0c;宽度等宽横向排列 <!DOCTYPE html> <html lang"en"> <head><meta chars…

.移动端适配的解决方案

何为移动端适配 移动端适配就是值在不同的移动端 可以去讲我们的内容适应不同屏幕尺寸大小 我们之前写单位用的是px这个单位 但是这是一个写死的单位 rem 所以我们用一个可变的单位 rem &#xff08;是指用html字体大小作为单位 比如说我们设置html字体大小为16px 那么 …

移动web适配

当屏幕宽度发生变化时&#xff0c;页面元素的尺寸&#xff08;宽度和高度&#xff09;也会随之变化&#xff0c;为更好的达到适配效果&#xff0c;用户体验更好&#xff0c;百分比布局 和 Flex布局 是有缺陷的&#xff0c;不能完成最终的适配。想要解决检测屏幕大小的问题&…

FTP-Web端如何直接访问FTP资源

ftp客户端工具&#xff1a;iis7服务器管理工具 IIs7服务器管理工具可以批量管理ftp站点&#xff0c;同时具备定时上传下载的功能。 作为服务器集成管理器&#xff0c;它最优秀的功能就是批量管理windows与linux系统服务器、vps。能极大的提高站长及服务器运维人员工作效率。同…

移动端适配的理解和各种方案解析(详解)

-&#x1f482; 个人网站:【紫陌】【笔记分享网】&#x1f485; 想寻找共同学习交流、共同成长的伙伴&#xff0c;请点击【前端学习交流群】 前言&#xff1a;最近在弄移动端项目&#xff0c;记录一下移动端的应用方案。对各个方案的解决理解。 目录 1.什么是移动端适配 2.理解…

手机上安装FTP客户端软件(AndFTP),实现通过手机访问计算机FTP服务器

服务器连接工具&#xff1a; IIS7服务器管理工具是一款windows全系下用于连接并操控基于windows和linux系统的VPS、VNC、FTP等远程服务器、云服务器的管理工具。 界面简单明了&#xff0c;操作易上手&#xff0c;功能强大&#xff0c;支持批量导入服务器&#xff0c;并批量打开…

pc端与移动端适配 解决方案

一般网站实现pc端与移动端适配的需求&#xff0c;方案有两个&#xff1a; 1、一套页面&#xff0c;从设计时就考虑到跨设备适配&#xff0c;响应式的一步到位&#xff1b; 2、开发两套页面&#xff0c;根据设备尺寸加载加载不同的资源&#xff0c;目前已经不常见了&#xff1…

【14.HTML-移动端适配】

移动端适配 1 布局视口和视觉视口1.1 设置移动端布局视口宽度 2 移动端适配方案2.1 rem单位动态html的font-size&#xff1b;2.2 vw单位2.3 rem和vw对比2.4 flex的弹性布局 1 布局视口和视觉视口 1.1 设置移动端布局视口宽度 避免布局视口宽度默认980px带了的缩放问题,并且禁止…

移动端常见适配方案

基础 网上已经有非常多的基础知识总结&#xff0c;不再赘诉&#xff0c;详情可以见 《关于移动端适配&#xff0c;你必须要知道的》 《不要再问我移动适配的问题了》 其中容易搞混的概念是视口 <meta name"viewport" content"widthdevice-width,user-sc…

FTP服务应用(手机端与电脑端无线传输)

FTP服务应用&#xff08;手机端与电脑端无线传输&#xff09; 准备工具&#xff1a;Android手机.KSWEB软件。 1.利用Android手机.打开移动网络共享。 2.电脑连上WiFi热点。 3.Android手机安装KSWEB软件&#xff0c;并打开FTP服务器。 4.在KSWEB软件左划到FTP模块&#xff0c;将…

移动端适配方案有哪几种?

虽然我们课程明确的区分各种移动端适配方案&#xff0c;但依然有很多同学搞不清楚移动端等比适配和响应式&#xff0c;这里对移动端主流适配方案给大家做一个分析。 移动端适配是指同一个页面可以在不同的移动端设备上都有合理的布局。主流的实现方案有两种&#xff1a; 响应…

solr简介和使用

一、搜索功能的流行方案 由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有集中方案可供选择: 1、基于Lucene自己进行封装实现站内搜索。工作量及扩展性都较大,不采用。 2、调用Google、Baidu的…

solr 安装和使用

Solr是基于ApacheLucene构建的流行、快速、开源的企业搜索平台 Solr具有高度可靠性、可扩展性和容错性&#xff0c;提供分布式索引、复制和负载平衡查询、自动故障切换和恢复、集中配置等功能。Solr为许多世界上最大的互联网站点提供搜索和导航功能 环境准备 linux centos7 ja…

什么是Solr,它能为我们解决什么问题,怎么用?

一. 什么是Solr? 其实我们大多数人都使用过Solr,也许你不会相信我说的这句话,但是事实却是如此啊 ! 每当你想买自己喜欢的东东时,你可能会打开某宝或者某东,像这样一搜,就能搜到很多东西,你知道你看到的这些数据都来自哪儿吗?百度一下你就知道!这些数据来自哪儿吗?等你了解…

solr实现原理

solr那是我1年前使用到的一个搜索引擎&#xff0c;由于当初对于配置了相应了&#xff0c;但是今天突然面试问到了&#xff0c;哎&#xff0c;太久了&#xff0c;真的忘记了&#xff0c;今天特地写一篇博客记下来 solr是一个独立的企业级搜索应用服务器&#xff0c;它对外t提供…

solr的使用详解

一、Solr简介 由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求&#xff0c;目前在实现搜索引擎的方案上有几种方案可供选择&#xff1a; 基于Lucene自己进行封装实现站内搜索。工作量及扩展性都较大&#xff0c;不采用。 调用Go…

solr基础理解和功能分析

一、solr概述 Solr是一个开源搜索平台&#xff0c;用于构建搜索应用程序。 它建立在Lucene(全文搜索引擎)之上。Solr是一个可扩展的&#xff0c;可部署&#xff0c;搜索/存储引擎&#xff0c;优化搜索大量以文本为中心的数据。 二、solr管理界面功能 1.Logging 展示Solr的日…

Solr基本概念

Solr是一种开放源码的、基于Lucene的搜索服务器。它易于安装和配置&#xff0c;而且附带了一个基于HTTP 的管理界面。 官网&#xff1a; http://lucene.apache.org/solr/ Solr全文检索基本原理&#xff1a; http://www.importnew.com/12707.html 相关概念&#xff1a; Core: …

Solr 原理、API 使用

日萌社 人工智能AI&#xff1a;Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战&#xff08;不定时更新&#xff09; 搜索引擎&#xff1a;Elasticsearch、Solr、Lucene ELK中的ES&#xff1a;ElasticsearchSolrCloud 的搭建、使用Solr 高亮显示Spring Data Solr …