void Button3Click(object sender, EventArgs e) { IPHostEntry MyEntry = Dns.GetHostByName(Dns.GetHostName()); IPAddress MyAddress = new IPAddress(MyEntry.AddressList[0].Address); string ip = MyAddress.ToString(); string msg = "今日发布\n"; if (this.exListView1.SelectedItems.Count > 0) { string[] packidarray = new string[this.exListView1.SelectedItems.Count]; for (int i = 0; i < this.exListView1.SelectedItems.Count; i++) { string name = this.exListView1.SelectedItems[i].SubItems[0].Text; string path = this.exListView1.SelectedItems[i].SubItems[1].Text; msg += (i + 1).ToString() + "、"; msg += name + "\n"; packidarray[i] = this.exListView1.SelectedItems[i].SubItems[5].Text; } msg += "需要更新的项目包括:\n"; List <ProjectInfo> prolist = ProjectInfoDao.getAllProjectInfoByPackID(packidarray); for (int j = 0; j < prolist.Count; j++) { msg += (j + 1).ToString() + "、"; msg += prolist[j].Projectname + ":" + ftphost.Insert(6, username + ":" + password + "@") + "/" + prolist[j].Ftppath + "\n"; } //MessageBox.Show(msg); Clipboard.SetDataObject(msg, true); MessageBox.Show("发布信息成功复制到剪贴板", "提示"); } }
/// <summary> /// 上传具体方法,根据关联的项目数循环上传至FTP /// </summary> /// <param name="filename">被上传的文件路径</param> /// <param name="serverpath">上传的项目所在的相对路径</param> /// <param name="pp">进度条控件</param> private void Upload1(string filename, string serverpath, ProgressBar pp) { //读取区间大小 int buffLength = 2048; byte[] buffer; FtpWebRequest reqFTP; FtpWebResponse uploadResponse = null; //上传数据流 Stream strm = null; //本地文件流 FileStream fs = null; //获取文件信息 FileInfo fileInf = new FileInfo(filename); try { if (!fileInf.Exists) { MessageBox.Show("文件不存在", "提示"); return; } } catch (Exception e1) { MessageBox.Show(e1.ToString(), "提示"); return; } ListViewItem item = (ListViewItem)pp.Tag; LinkLabel l = ((LinkLabel)((EXControlListViewSubItem)item.SubItems[4]).MyControl); del_do_update delupdate = new del_do_update(do_update); del_do_changetxt delchangetxt = new del_do_changetxt(ChangeTextMethod); //查询要上传至服务器的项目数 List <ProjectInfo> _prjlist = new List <ProjectInfo>(); if (!string.IsNullOrEmpty(item.SubItems[5].Text)) { _prjlist = ProjectInfoDao.getAllProjectInfoByPackID(new string[] { item.SubItems[5].Text }); } for (int i = 0; i < _prjlist.Count; i++) { if (!bool.Parse(item.Tag as string)) //中断 { break; } try { this.MakeDirectory(ftphost + _prjlist[i].Ftppath + serverpath); } catch (Exception) { MessageBox.Show("创建文件夹错误,可能目标文件夹已经存在", "提示"); } string uri = ftphost + _prjlist[i].Ftppath + serverpath + fileInf.Name; try { // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(this.username, this.password); reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy(); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb buffer = new byte[buffLength]; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 fs = fileInf.OpenRead(); strm = reqFTP.GetRequestStream(); int bytesRead; int hasread = 0; pp.BeginInvoke(delupdate, new object[] { pp, hasread, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname }); while (bool.Parse(item.Tag as string)) //中断 { //Thread.Sleep(300); bytesRead = fs.Read(buffer, 0, buffer.Length); hasread += bytesRead; if (bytesRead == 0) { break; } strm.Write(buffer, 0, bytesRead); pp.BeginInvoke(delupdate, new object[] { pp, hasread, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname }); } fs.Close(); strm.Close(); uploadResponse = (FtpWebResponse)reqFTP.GetResponse(); } catch (Exception e) { MessageBox.Show("上传到" + uri + "发生错误:" + e.ToString(), "提示"); pp.BeginInvoke(delupdate, new object[] { pp, (int)fileInf.Length, (int)fileInf.Length, i + 1, _prjlist.Count, _prjlist[i].Projectname }); } } l.BeginInvoke(delchangetxt, new object[] { l, "重传" }); if (uploadResponse != null) { uploadResponse.Close(); } if (fs != null) { fs.Close(); } if (strm != null) { strm.Close(); } }