概述
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]}KBn文件类型: {details[3]}";
}
}
}
}
}
最后
以上就是聪明凉面为你收集整理的FTP客户端和服务器的设计与实现的全部内容,希望文章能够帮你解决FTP客户端和服务器的设计与实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复