|
@@ -0,0 +1,190 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Text;
|
|
|
+using System.Windows.Forms;
|
|
|
+using UAS_PRINT.Properties;
|
|
|
+
|
|
|
+namespace UAS_PRINT
|
|
|
+{
|
|
|
+ class ftpOperater
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ public static bool Inner = false;
|
|
|
+
|
|
|
+ public static string DownLoadTo = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
+
|
|
|
+ private string ftpServerIP;
|
|
|
+ private string ftpUser;
|
|
|
+ private string ftpPwd;
|
|
|
+
|
|
|
+ public ftpOperater(bool PortModel)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ this.ftpServerIP = Settings.Default.ftpaddress;
|
|
|
+ this.ftpUser = Settings.Default.ftpuser;
|
|
|
+ this.ftpPwd = Settings.Default.ftppassword;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ {
|
|
|
+
|
|
|
+ dataRead = fs.Read(content, 0, BufferSize);
|
|
|
+ rs.Write(content, 0, dataRead);
|
|
|
+ } while (!(dataRead < BufferSize));
|
|
|
+ rs.Close();
|
|
|
+ }
|
|
|
+ fs.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|