Ftp进行文件的上传和下载

  • 格式:pdf
  • 大小:73.36 KB
  • 文档页数:4

Ftp进⾏⽂件的上传和下载

这款软件功能丰富,主要有:

1、⾃动重连

2、⾃动重传

3、定时任务 (定时上传、定时下载)

4、⾃定义传输模式,线程,编码

5、删除到回收站

6、⼤量⽂件快速加载,边加载边传输

7、批量连接⼀键关闭

接下来来欣赏下界⾯:

⼆、下⾯是.net 常⽤的⽂件上传帮助类:

下载:

⽂件操作类using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Windows.Forms;

using System.Configuration;

using System.Net;

namespace FTP⽂件上传

{

class FileOperate

{

private static FileOperate _fileOperate = null;

private string _filePath = string.Empty;

public string FilePath { get { return _filePath; } private set { _filePath = value; } }

private FileInfo fileInfo = null;

public string[] files = null;

public static FileOperate GetInstance()

{

if (_fileOperate == null)

_fileOperate = new FileOperate();

return _fileOperate;

}

public void OpenFile()

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "⽂本⽂件|*.*|C#⽂件|*.cs|所有⽂件|*.*|⽂件夹|*.File folder";

ofd.RestoreDirectory = true;

ofd.Multiselect = true;

ofd.FilterIndex = 1;

if (ofd.ShowDialog() == DialogResult.OK)

{

files = ofd.FileNames;

}

}

public void UpLoadFile(string LocalfileName)

{

if (string.IsNullOrEmpty(LocalfileName))

return;

string url = GetUrl(ConfigManager.HostName, ConfigManager.TargetDir);

System.Net.FtpWebRequest ftp = GetRequest(url);

//设置FTP命令 设置所要执⾏的FTP命令,

//ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显⽰指定路径下的⽂件列表

ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

fileInfo = new FileInfo(LocalfileName);

//指定⽂件传输的数据类型

ftp.UseBinary = true;

ftp.UsePassive = true;

//告诉ftp⽂件⼤⼩

ftp.ContentLength = fileInfo.Length;

//缓冲⼤⼩设置为2KB

const int BufferSize = 2048;

byte[] content = new byte[BufferSize - 1 + 1];

int dataRead;

//打开⼀个⽂件流 (System.IO.FileStream) 去读上传的⽂件

using (FileStream fs = fileInfo.OpenRead()) {

try

{

//把上传的⽂件写⼊流

using (Stream rs = ftp.GetRequestStream())

{

do

{

//每次读⽂件流的2KB

dataRead = fs.Read(content, 0, BufferSize);

rs.Write(content, 0, dataRead);

} while (!(dataRead < BufferSize));

rs.Close();

}

}

catch (Exception ex) { }

finally

{

fs.Close();

}

}

ftp = null;

//设置FTP命令

ftp = GetRequest(url);

ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名

ftp.RenameTo = fileInfo.Name;

try

{

ftp.GetResponse();

}

catch (Exception ex)

{

ftp = GetRequest(url);

ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除

ftp.GetResponse();

throw ex;

}

finally

{

ftp = null;

fileInfo.Delete();

}

// 可以记录⼀个⽇志 "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );

}

///

/// 下载⽂件

///

/// Ftp的⽂件⽬录

/// Ftp上的⽂件名称

/// 本地的⽂件⽬录

public void DownLoadFile(string FtpFilePath,string FtpFileName,string localFilePath)

{

if (!IsExistDir(FtpFilePath+"/"+FtpFileName))

return;

string url = "ftp://" + ConfigManager.HostName + FtpFilePath+"/"+FtpFileName;

FtpWebRequest ftp = GetRequest(url);

ftp.Method = WebRequestMethods.Ftp.DownloadFile;

ftp.UseBinary = true;

ftp.UsePassive = true;

using(FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())

{

using(Stream rsp = response.GetResponseStream())

{

localFilePath = localFilePath + @"\" + FtpFileName;

using(FileStream fs = new FileStream(localFilePath,FileMode.OpenOrCreate))

{

try

{

byte[] buffer = new byte[2048];

int read = 0;

do

{

read = rsp.Read(buffer, 0, buffer.Length);

fs.Write(buffer, 0, read);

} while (!(read == 0));

fs.Flush();

fs.Close();

}

catch

{

//失败删除⽂件

fs.Close();

fileInfo = new FileInfo(localFilePath);

fileInfo.Delete(); }

}

}

}

}

private static string GetUrl(string hostName, string targetDir)

{

string target = Guid.NewGuid().ToString();

return "FTP://" + hostName + "/" + targetDir + "/" + target;

}

private static FtpWebRequest GetRequest(string url)

{

//根据服务器信息FtpWebRequest创建类的对象

FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(url);

//提供⾝份验证信息

result.Credentials = new System.Net.NetworkCredential(ConfigManager.UserName, ConfigManager.Password);

//设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true

result.KeepAlive = false;

return result;

}

public List GetDirectorysAndFiles(string requestPath)

{

var dict = new List();

if (!IsExistDir(requestPath))

return dict;

try

{

string url = "ftp://" + ConfigManager.HostName + requestPath;

FtpWebRequest ftp = GetRequest(url);

ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

var response = ftp.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

line = line.Substring(line.LastIndexOf(' ') + 1);

dict.Add(line);

line = reader.ReadLine();

}

ftp = null;

response.Close();

}

catch { }

return dict;

}

private Boolean IsExistDir(string requestPath)

{

try

{

string url = "ftp://" + ConfigManager.HostName + "/" + requestPath;