using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace UAS_LabelMachine
{
class ftpOperater
{
public static string FTPAddress = DataHelper.FTPAdress.Split('|')[0];
public static string DownLoadTo = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\" + @"打印标签\";
private string ftpServerIP;
private string ftpUser;
private string ftpPwd;
public ftpOperater()
{
string[] FTPInf = DataHelper.FTPAdress.Split('|');
this.ftpServerIP = FTPInf[0];
this.ftpUser = FTPInf[1];
this.ftpPwd = FTPInf[2];
}
public void UpLoadFile(string filepath, string filename)
{
//上传之前判断文件是否存在
string[] filelist = GetFileList();
if (filelist != null)
for (int i = 0; i < filelist.Length; i++)
{
if (filelist[i] == filename)
{
string upload = MessageBox.Show("已存在同名文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
if (upload.ToString() != "Yes")
{
return;
}
}
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo file = new FileInfo(filepath + "/" + filename);
const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead;
using (FileStream fs = file.OpenRead())
{
//把上传的文件写入流
using (Stream rs = reqFTP.GetRequestStream())
{
do
{
//每次读文件流的2KB
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
} while (!(dataRead < BufferSize));
rs.Close();
}
fs.Close();
}
}
///
/// 获取ftp服务器上的文件信息
///
/// 存储了所有文件信息的字符串数组
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
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)
{
System.Windows.Forms.MessageBox.Show("获取文件信息失败:" + ex.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
downloadFiles = null;
return downloadFiles;
}
}
///
/// 获取FTP上指定文件的大小
///
/// 文件名
/// 文件大小
public long GetFileSize(string filename)
{
FtpWebRequest reqFTP;
long fileSize = 0;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return fileSize;
}
///
/// 实现ftp下载操作
///
/// 远程文件名
public string Download(string fileName, DateTime time)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
reqFTP.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 4096;
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();
//设置最后修改文件时间为服务器时间
FileInfo f = new FileInfo(DownLoadTo + @"\" + fileName);
f.LastWriteTime = time;
return DownLoadTo + @"\" + fileName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}
}
}