C#实现客户端与服务器端的文件传输
实现文件的异地传输,可以采用http协议 ftp等协议;ftp传输的原理同样也是数据流的IO数据交换。关键的4步有:1.客户端与服务端的连接;2.客户端的文件流转换;
3.I/O数据传输;4.服务端数据的写入.
- public class FtpWeb
- {
- string ftpRemotePath;
- string ftpUserID;
- string ftpPassword;
- string ftpURI;
- string ftpServerIP;
- /// <summary>
- /// 连接FTP
- /// </summary>
- /// <param name="FtpServerIP">FTP连接地址</param>
- /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
- /// <param name="FtpUserID">用户名</param>
- /// <param name="FtpPassword">密码</param>
- public FtpWeb(string FtpServerIP, string FtpUserID, string FtpPassword)
- {
- ftpServerIP = FtpServerIP;
- ftpUserID = FtpUserID;
- ftpPassword = FtpPassword;
- ftpURI = FtpServerIP + "/";
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="filename"></param>
- public void Upload(string filename)
- {
- FileInfo fileInf = new FileInfo(filename);
- string uri = ftpURI + fileInf.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 = 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();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="filename"></param>
- public void Upload2(string ftpuri, string filename)
- {
- FileInfo fileInf = new FileInfo(filename);
- string uri = ftpuri;
- 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 = 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();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 下载
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="fileName"></param>
- public void Download(string filePath, string ftpUri, string fileName)
- {
- FtpWebRequest reqFTP;
- try
- {
- FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUri));
- reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
- reqFTP.UseBinary = true;
- reqFTP.Credentials = new NetworkCredential(ftpUserID, 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 (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 删除文件
- /// </summary>
- /// <param name="fileName"></param>
- public void Delete(string fileName)
- {
- try
- {
- string uri = fileName;
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
- reqFTP.KeepAlive = false;
- reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
- 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();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 删除文件夹
- /// </summary>
- /// <param name="folderName"></param>
- public void RemoveDirectory(string folderName)
- {
- try
- {
- string uri = ftpURI + folderName;
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- reqFTP.Credentials = new