public static void EnsureParentDirExist(FTPUtil ftpUtil, string path) { var dir = Path.GetDirectoryName(path); var parents = new Queue <string>(); while (!string.IsNullOrEmpty(dir)) { parents.Enqueue(dir); dir = Path.GetDirectoryName(dir); } while (parents.Count > 0) { if (null != ftpUtil) { try { ftpUtil.MakeDirectory(parents.Dequeue()); } catch (Exception ex) { } } else { Debug.LogError("EnsureParentDirExist throw exception! FTPUtil is null!"); } } }
//删除文件 private void btnFTPDeleteFile_Click(object sender, EventArgs e) { if (lvFTP.SelectedItems.Count == 0) { return; } string ftpfile = getDir(tvFTP.SelectedNode); ftpfile += "/" + lvFTP.SelectedItems[0].Text; try { ftp = initFTP(); bool ok = ftp.DeletFile(ftpfile); ftp = null; if (ok) { tvFTP_AfterSelect(null, null); } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnFTPGetList_Click(object sender, EventArgs e) { try { FTPUtil ftp = initFTP(); List <string> lst = ftp.GetDirectoryList(""); tvFTP.Nodes.Clear(); foreach (string dir in lst) { tvFTP.Nodes.Add(dir); } lst = ftp.GetFileList(""); lvFTP.Items.Clear(); foreach (string file in lst) { lvFTP.Items.Add(file); } ftp = null; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//上传文件 private void btnFTPUpload_Click(object sender, EventArgs e) { string ftpfile = getDir(tvFTP.SelectedNode); if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } string file = openFileDialog1.FileName; FileInfo info = new FileInfo(file); ftpfile += "/" + info.Name; try { ftpMethod = "上传"; ftp = initFTP(); bool ok = ftp.UploadFile(file, ftpfile, brokenOpen: cbxFTPUseBroken.Checked, updateProgress: setFtpInfo); ftp = null; if (ok) { tvFTP_AfterSelect(null, null); } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnFTPDown_Click(object sender, EventArgs e) { if (lvFTP.SelectedItems.Count == 0) { return; } string ftpfile = getDir(tvFTP.SelectedNode); ftpfile += "/" + lvFTP.SelectedItems[0].Text; saveFileDialog1.FileName = lvFTP.SelectedItems[0].Text; if (saveFileDialog1.ShowDialog() != DialogResult.OK) { return; } string file = saveFileDialog1.FileName; try { ftpMethod = "下载"; ftp = initFTP(); bool ok = ftp.DownloadFile(ftpfile, file, brokenOpen: cbxFTPUseBroken.Checked, updateProgress: setFtpInfo); ftp = null; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void tvFTP_DoubleClick(object sender, EventArgs e) { try { string folder = getDir(tvFTP.SelectedNode); if (folder.Equals("")) { return; } FTPUtil ftp = initFTP(); List <string> lst = ftp.GetDirectoryList(folder); tvFTP.SelectedNode.Nodes.Clear(); foreach (string dir in lst) { tvFTP.SelectedNode.Nodes.Add(dir); } tvFTP.SelectedNode.ExpandAll(); /* lst = ftp.GetFileList(folder); * lvFTP.Items.Clear(); * foreach (string file in lst) * { * lvFTP.Items.Add(file); * } */ ftp = null; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//迁移DLL方法,安卓上传文件 //oErrMessage = route[0] + route[1] + route[2]; //public bool UploadFileAndroid(String FtpServerIP, String FtpUser, String FtpPsw, String MemoryStream,String fileName,out String oErrMessage) public bool UploadFileAndroid(String FtpServerIP, String FtpUser, String FtpPsw, byte[] bytes, String filename, out String oErrMessage) { oErrMessage = null; //FTP目录 String FTPDirectory; try { //连接FTP FTPUtil ftpUtil = new FTPUtil(FtpServerIP, "", FtpUser, FtpPsw); //时间 string NowDate = db.GetColumnObject("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual").ToString(); string ServerTime = db.GetColumnObject("select to_char(sysdate,'yyyy-MM-dd-hh24') as nowTime from dual").ToString(); string[] route = ServerTime.Split(new char[] { '-' }, StringSplitOptions.None); FTPDirectory = route[0] + route[1] + route[2]; //文件路径 ftpUtil.GotoDirectory(FTPDirectory, true); oErrMessage = "0"; FtpWebRequest frequest = null; if (!ftpUtil.FileExist_1(ftpUtil.ftpuri)) { frequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUtil.ftpuri)); frequest.Credentials = new NetworkCredential(FtpUser, FtpPsw); frequest.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse response = frequest.GetResponse() as FtpWebResponse; //frequest.KeepAlive = false; } oErrMessage = "11"; FtpWebRequest frequest1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUtil.ftpuri + "/" + filename)); frequest1.Credentials = new NetworkCredential(FtpUser, FtpPsw); oErrMessage = "12"; frequest1.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行 oErrMessage = "14"; //frequest.KeepAlive = false; oErrMessage = "13"; frequest1.Method = WebRequestMethods.Ftp.UploadFile; // 指定执行什么命令 oErrMessage = "15"; frequest1.UseBinary = true; // 指定数据传输类型 frequest1.ContentLength = bytes.Length; // 上传文件时通知服务器文件的大小 Stream strm = frequest1.GetRequestStream(); strm.Write(bytes, 0, bytes.Length); strm.Close(); oErrMessage = "2"; return(true); } catch (Exception ex) { oErrMessage = "错误信息" + oErrMessage + ex.Message; return(false); } }
private FTPUtil initFTP() { string host = edtFTPHost.Text; int port = 21; int.TryParse(edtFTPPort.Text, out port); string user = edtFtpUser.Text; string psd = edtFtpPassword.Text; FTPUtil ftp = new FTPUtil(host, port, user, psd, "", rbtnFTPPASV.Checked, cbxFTPSSL.Checked); return(ftp); }
//删除文件夹 private void btnFTPDeleteDir_Click(object sender, EventArgs e) { string ftpfile = getDir(tvFTP.SelectedNode); try { ftp = initFTP(); ftp.DelDir(ftpfile); ftp = null; tvFTP.SelectedNode.Remove(); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//下载图片并显示出来 internal static bool IF_Directory(string str_dir) { try { if (main.M_var.IURL != null) { if (!Directory.Exists(str_dir)) { //判断有没有路径 Directory.CreateDirectory(str_dir); //获取ftpIP,用户,密码 String strsql = "select ftp_ip,ftp_user,ftp_pwd from MES_SOP_FTP a where rownum=1"; object[] obj_1 = methods.Serjavaweb.getColumnStringAll(strsql); String[] str_p_LineName = obj_1[0].ToString().Split('['); //登录FTP FTPUtil ftp = new FTPUtil(str_p_LineName[1].ToString(), "", str_p_LineName[2].ToString(), str_p_LineName[3].ToString()); //开始下载 ftp.Download_dz(main.M_var.IURL, str_dir + "\\SOP下载.jpg"); return(true); } else { //获取ftpIP,用户,密码 String strsql = "select ftp_ip,ftp_user,ftp_pwd from MES_SOP_FTP a where rownum=1"; object[] obj_1 = methods.Serjavaweb.getColumnStringAll(strsql); String[] str_p_LineName = obj_1[0].ToString().Split('['); //登录FTP FTPUtil ftp = new FTPUtil(str_p_LineName[1].ToString(), "", str_p_LineName[2].ToString(), str_p_LineName[3].ToString()); //开始下载 ftp.Download_dz(main.M_var.IURL, str_dir + "\\SOP下载.jpg"); return(true); } } else { MessageBox.Show("获取图片失败路径为空"); return(false); } } catch (Exception ex) { MessageBox.Show("获取图片失败请检查维护SOP是否与此MAC绑定岗位一致" + ex.Message); return(false); } }
private void tvFTP_AfterSelect(object sender, TreeViewEventArgs e) { try { string folder = getDir(tvFTP.SelectedNode); FTPUtil ftp = initFTP(); List <string> lst = ftp.GetFileList(folder); lvFTP.Items.Clear(); foreach (string file in lst) { ListViewItem item = lvFTP.Items.Add(file); string size = StringUtil.formatBytes(ftp.GetFileSize(folder + "/" + file)); item.SubItems.Add(ftp.GetFileSize(folder + "/" + file).ToString() + " (" + size + ")"); } ftp = null; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// UpLoadCDN /// </summary> /// <param name="buildTargetGroup"></param> private static void UpLoadCDN(BuildTargetGroup buildTargetGroup) { var beginTime = System.DateTime.Now; var isMotherPkg = ContainsEnvOption(EnvOption.MOTHER_PKG); var isHotUpdateBuild = ContainsEnvOption(EnvOption.HOT_UPDATE_BUILD); if (isHotUpdateBuild || isMotherPkg) { //上传远端CDN if (ContainsEnvOption(EnvOption.REMOTE_CDN)) { var host = GetEnvironmentVariable(EnvOption.CDN_URL); var userName = GetEnvironmentVariable(EnvOption.CDN_USERNAME); var password = GetEnvironmentVariable(EnvOption.CDN_PASSWORD); var ftpUtil = new FTPUtil(host, userName, password); //复制到本地目录以后打成zip,然后把zip上传到CDN //upload appversion.json var tempCDNRoot = "ColaCache/CDN"; FileHelper.RmDir(tempCDNRoot); var cachePath = ColaEditHelper.TempCachePath + "/" + AppVersionFileName; var CDN_AppVersionPath = string.Format(ColaEditHelper.TempCachePath + "/" + CDNVersionControlUrl, ColaEditHelper.GetPlatformName(), "app_version.json"); FileHelper.CopyFile(cachePath, CDN_AppVersionPath, true); //upload version.txt and assets var reltaRoot = ColaEditHelper.CreateAssetBundleDirectory(); var updateFilePath = reltaRoot + "/updates.txt"; using (var sr = new StreamReader(updateFilePath)) { var content = sr.ReadLine(); while (null != content) { var reltaPath = reltaRoot + "/" + content; var destPath = ColaEditHelper.TempCachePath + "/" + CDNResourceUrl + content; FileHelper.CopyFile(reltaPath, destPath, true); content = sr.ReadLine(); } } FileHelper.CopyFile(reltaRoot + "/versions.txt", ColaEditHelper.TempCachePath + "/" + CDNResourceUrl + "versions.txt", true); //ZIP var zipPath = "ColaCache/hotupdate.zip"; FileHelper.DeleteFile(zipPath); var result = ZipHelper.Zip(tempCDNRoot, zipPath); if (result) { ftpUtil.Upload(zipPath, "Upload/hotupdate.zip"); } else { Debug.LogError("Zip并上传CDN过程中出现错误!"); } } //上传本地CDN,打包机和CDN是同一台机器 else { //upload appversion.json var cachePath = ColaEditHelper.TempCachePath + "/" + AppVersionFileName; var CDN_AppVersionPath = string.Format(CDNVersionControlUrl, ColaEditHelper.GetPlatformName(), "app_version.json"); FileHelper.CopyFile(cachePath, CDN_AppVersionPath, true); //upload version.txt and assets var reltaRoot = ColaEditHelper.CreateAssetBundleDirectory(); var updateFilePath = reltaRoot + "/updates.txt"; using (var sr = new StreamReader(updateFilePath)) { var content = sr.ReadLine(); while (null != content) { var reltaPath = reltaRoot + "/" + content; var destPath = CDNResourceUrl + content; FileHelper.CopyFile(reltaPath, destPath, true); content = sr.ReadLine(); } } FileHelper.CopyFile(reltaRoot + "/versions.txt", CDNResourceUrl + "versions.txt", true); } } Debug.Log("=================UpLoadCDN Time================ : " + (System.DateTime.Now - beginTime).TotalSeconds); }
/// <summary> /// Method to upload the specified file to the specified FTP Server /// </summary> /// <param name="filename">file full name to be uploaded</param> private void Upload(string filename) { FTPUtil ftpUtil = new FTPUtil(); ftpUtil.FTPServerAddress = "www.qtc.vn"; ftpUtil.FTPUserID = "*****@*****.**"; ftpUtil.FTPUserPassword = "******"; ftpUtil.BufferLength = 2048; try { //FTPUtil.Upload("www.qtc.vn", "*****@*****.**", "Qtc333", "c:\\Downloads\\weasel.pdf"); ftpUtil.InitStreamToUpload("c:\\downloads\\weasel.pdf"); ftpUtil.UploadAllContent(); } catch (Exception exp) { MessageBox.Show(exp.Message); } finally { ftpUtil.ReleaseStream(); } //FTPUtil.Upload("www.qtc.vn", "*****@*****.**", "Qtc333", "c:\\Downloads\\weasel.pdf"); //QTC.LIB.Util.FTPUtil.Upload(ftpServerIP, ftpUserID, ftpPassword, filename); //FileInfo fileInf = new FileInfo(filename); //string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; //FtpWebRequest reqFTP; //// Create FtpWebRequest object from the Uri provided //reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name)); //// Provide the WebPermission Credintials //reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); //// By default KeepAlive is true, where the control connection is not closed //// after a command is executed. //reqFTP.KeepAlive = false; //// Specify the command to be executed. //reqFTP.Method = WebRequestMethods.Ftp.UploadFile; //// Specify the data transfer type. //reqFTP.UseBinary = true; //// Notify the server about the size of the uploaded file //reqFTP.ContentLength = fileInf.Length; //// The buffer size is set to 2kb //int buffLength = 2048; //byte[] buff = new byte[buffLength]; //int contentLen; //// Opens a file stream (System.IO.FileStream) to read the file to be uploaded //FileStream fs = fileInf.OpenRead(); //try //{ // // Stream to which the file to be upload is written // Stream strm = reqFTP.GetRequestStream(); // // Read from the file stream 2kb at a time // contentLen = fs.Read(buff, 0, buffLength); // // Till Stream content ends // while (contentLen != 0) // { // // Write Content from the file stream to the FTP Upload Stream // strm.Write(buff, 0, contentLen); // contentLen = fs.Read(buff, 0, buffLength); // } // // Close the file stream and the Request Stream // strm.Close(); // fs.Close(); //} //catch(Exception ex) //{ // MessageBox.Show(ex.Message, "Upload Error"); //} }