目录
- 1、初始化FTP
- 2、上传文件
- 3、下载文件
- 4、代码描述
我们使用C# 需要通过FTP访问服务器时,那么上位机就要作为客户端,通过连接FTP服务来上传下载文件。这里用实际代码和简要的文字描述一下。
1、初始化FTP
见代码描述段中的构造函数 FtpOperation(),在初始化类的同时将用户各个参数初始化。
首先,将服务器IP传递过来,这个是第一重要的。其次,将用户ID、密码以及路径信息获取。最后,将其它参数赋初值。
2、上传文件
上传文件,即,将本地的文件拷贝到服务器上。封装了的接口DoUpload(),调用之前要陪将对应的本地路径参数获取赋值,这里为一个全路径参数。
参考上传实现函数
private void UploadFile(string FileName)
3、下载文件
下载文件,即,将服务器上的文件拷贝到本地。封装了的接口DoDownload(),调用之前要陪将对应的本地路径参数获取赋值,这里包括要下载的文件名以及本地全路径名。
参数看下载实现函数
public void DownloadFile(string FileName, string LocalSavePath)
4、代码描述
class FtpOperation
{
private string ftpServerIP;
private string ftpRemotePath;
private string ftpUserID;
private string ftpPassword;
private string ftpURI;
private int BUF_SIZE;
public string ftp_local_path;
public string ftp_local_name;
public bool error_state;
public FtpOperation(string ServerIP)
{
ftpServerIP = ServerIP;
ftpRemotePath = "";
ftpUserID = "ftpuser";
ftpPassword = "pswd";
ftpURI = "ftp://" + ftpServerIP + ftpRemotePath + "/";
ftp_local_path = "";
ftp_local_name = "";
error_state = false;
}
public void DoUpload()
{
UploadFile(ftp_local_path);
}
public void DoDownload()
{
DownloadFile(ftp_local_name, ftp_local_path);
}
private void UploadFile(string FileName)
{
FileInfo fileInfo = new FileInfo(FileName);
string uri = ftpURI + fileInfo.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInfo.Length;
BUF_SIZE = (int)fileInfo.Length;
reqFTP.Timeout = 30000;
byte[] buff = new byte[BUF_SIZE];
int ContentLength;
FileStream fs = fileInfo.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
ContentLength = fs.Read(buff, 0, BUF_SIZE);
while (ContentLength != 0)
{
strm.Write(buff, 0, ContentLength);
ContentLength = fs.Read(buff, 0, BUF_SIZE);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
error_state = true;
MessageBox.Show(ex.Message);
}
}
public void DownloadFile(string FileName, string LocalSavePath)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(LocalSavePath, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpUserID + ":" + ftpPassword + "@" + ftpServerIP + "/" + FileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
BUF_SIZE = (int)response.ContentLength;
int readCount;
byte[] buffer = new byte[BUF_SIZE];
readCount = ftpStream.Read(buffer, 0, BUF_SIZE);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, BUF_SIZE);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
error_state = true;
MessageBox.Show(ex.Message);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)