Ftp进行文件的上传和下载
2021-03-27 22:24
标签:dex mes keepalive void tar private ISE result files Ftp进行文件的上传和下载 标签:dex mes keepalive void tar private ISE result files 原文地址:https://www.cnblogs.com/hnzheng/p/12627054.html下载:
文件操作类
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
/// 下载文件
///
/// 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