项目总结
前段时间一直在搞一个:tcp/ip异步 传输 图片,excel,word,pdf并显示在winform中的一个小项目。
总结下之前遇到的一些问题
1,起初,我的思路是,在服务器端建设一个web服务器,服务器端向客户端发送url地址,客户端使用webbrowser在winform中显示出来。
按照这种思路一直走了下去。因为无法控制网络的连接状态。
2,因为某些原因:将文件分段传输到客户端,然后分别打开显示。
期间的一些问题及处理方法做一个分享部分代码:
1,文件的文段传输
View Code
string path = @"F:\Website\Machine\" + comboBox2.Text + "\\" + comboBox1.Text + "\\" + item.ToString(); string[] temp = path.Split('\\'); string Filename = temp[temp.Length - 1].ToString(); recesucess = false; FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read); FileByteArray = Encoding.UTF8.GetBytes(Filename); FileByteArraySend = null; FileByteArraySend = new byte[FileByteArray.Length + filenameArray.Length + eofArray.Length]; FileByteArray.CopyTo(FileByteArraySend, 0); filenameArray.CopyTo(FileByteArraySend, FileByteArray.Length);//合并byte数组 eofArray.CopyTo(FileByteArraySend, FileByteArray.Length + filenameArray.Length); long Length = fs.Length; LengthByteArray = Encoding.ASCII.GetBytes(Length.ToString()); lengthByteArraySend = null; lengthByteArraySend = new byte[LengthByteArray.Length + filelengthArray.Length + eofArray.Length]; LengthByteArray.CopyTo(lengthByteArraySend, 0); filelengthArray.CopyTo(lengthByteArraySend, LengthByteArray.Length);//合并byte数组 eofArray.CopyTo(lengthByteArraySend, LengthByteArray.Length + filelengthArray.Length); bool flg = false; jpgByteArray = new byte[CONSTANTS.SERVERMSGMAXLEN - 10]; jpgByteArraySend = new byte[CONSTANTS.SERVERMSGMAXLEN]; //文件大小 long PocketLength = fs.Length; //包大小 int PocketSize = CONSTANTS.SERVERMSGMAXLEN - 10; //包数量 long PocketCount = PocketLength / (long)PocketSize; //最后一个包的大小 long LastPocket = PocketLength - (long)PocketCount * PocketSize; //获取“文件名”编码组 byte[] filename = Encoding.UTF8.GetBytes(label4.Text); //获得“文件长度”编码组 byte[] filelength = Encoding.ASCII.GetBytes(PocketLength.ToString()); //FileStream pic = new FileStream("F:\\S_picdata.txt", FileMode.OpenOrCreate, FileAccess.Write); recesucess = false; BreakFlag = false; //发送文件长度 if (SendByteArr(socketinfo[socket_i].getsocket, lengthByteArraySend) == true) { this.times.Enabled = true;//开始监视 Timer1counter = 0; while (recesucess == false) { Application.DoEvents(); } this.times.Enabled = false; if (BreakFlag) throw new Exception(this.listBox2.Items[listbox2_n].ToString() + "岗位发送失败!"); this.toolStripStatusLabel2.Text = this.listBox2.Items[listbox2_n].ToString() + "岗位:文件长度发送成功!"; this.toolStripStatusLabel1.Text = ""; } recesucess = false; BreakFlag = false; //发送文件名 if (SendByteArr(socketinfo[socket_i].getsocket, FileByteArraySend) == true) { this.toolStripStatusLabel2.Text = this.listBox2.Items[listbox2_n].ToString() + "岗位:文件名发送成功!"; this.times.Enabled = true;//开始监视 Timer1counter = 0; while (recesucess == false) { Application.DoEvents(); } if (BreakFlag) throw new Exception(this.listBox2.Items[listbox2_n].ToString() + "岗位发送失败!"); this.times.Enabled = false; } int jj = 65536; while (jj-- > 0) { Application.DoEvents(); } this.toolStripProgressBar1.Maximum = int.Parse(PocketLength.ToString()) + (int.Parse(PocketCount.ToString()) + 1) * 10; this.toolStripProgressBar1.Value = 0; this.toolStripStatusLabel2.Text = "正在发送" + this.listBox2.Items[listbox2_n].ToString() + "岗位"; //发送文件内容 for (int i = 0; i <= PocketCount; i++) { //判断是否为最后一个包 if (i < PocketCount) fs.Read(jpgByteArray, 0, jpgByteArray.Length);//读取文件流到数组 else { jpgByteArray = new byte[LastPocket]; jpgByteArraySend = new byte[LastPocket + 10]; fs.Read(jpgByteArray, 0, jpgByteArray.Length); } jpgByteArray.CopyTo(jpgByteArraySend, 0); blockArray.CopyTo(jpgByteArraySend, jpgByteArray.Length); eofArray.CopyTo(jpgByteArraySend, jpgByteArray.Length + blockArray.Length); recesucess = false; BreakFlag = false; if (i == PocketCount) { if (SendByteArr(socketinfo[socket_i].getsocket, jpgByteArraySend) == true) //发送数组包 { this.times.Enabled = true;//开始监视 Timer1counter = 0; while (recesucess == false) { Application.DoEvents(); } this.times.Enabled = false; if (BreakFlag) throw new Exception(this.listBox2.Items[listbox2_n].ToString() + "岗位发送失败!"); this.toolStripProgressBar1.Value += jpgByteArraySend.Length; this.toolStripStatusLabel2.Text = this.listBox2.Items[listbox2_n].ToString() + "文件发送成功!"; this.label15.Text = "已发送:第" + this.listBox2.Items[listbox2_n].ToString() + "岗位"; label15.Refresh(); flg = true; jj = 10000; while (jj-- > 0) { Application.DoEvents(); } } else { throw new Exception("最后一个包发送失败!"); } } else { if (SendByteArr(socketinfo[socket_i].getsocket, jpgByteArraySend) == true) { this.times.Enabled = true;//开始监视 Timer1counter = 0; while (recesucess == false) { Application.DoEvents(); } this.times.Enabled = false; if (BreakFlag) throw new Exception(this.listBox2.Items[listbox2_n].ToString() + "岗位发送失败!"); this.toolStripProgressBar1.Value += jpgByteArraySend.Length; jj = 10000; while (jj-- > 0) { Application.DoEvents(); } } else { throw new Exception("出现丢包现象,请重发!"); } } } if (flg == true) { this.times.Enabled = true;//开始监视 Timer1counter = 0; while (recesucess == false) { Application.DoEvents(); } this.times.Enabled = false; } else { MessageBox.Show("第" + this.listBox2.Items[listbox2_n].ToString() + "岗位图片数据发送不成功,请检查是否连接上1"); this.toolStripStatusLabel2.Text = "第" + this.listBox2.Items[listbox2_n].ToString() + "岗位图片数据发送不成功1"; this.listBox1.Items.RemoveAt(this.listBox1.Items.IndexOf(this.listBox2.Items[listbox2_n].ToString()));//删除一条lisbox1 ip信息 this.listBox2.Items.RemoveAt(listbox2_n);//删除一条lisbox2 ip信息 ConnectCnt = ConnectCnt - 1;//连接数减1 this.toolStripStatusLabel1.Text = "已经有" + ConnectCnt + "台主机连接上"; }
2,文件接收
View Code
if (IsFilelengthOverArray(MsgBuffer, REnd) == true) { loadsecuss = true; bufferByte = new byte[REnd]; Array.Copy(MsgBuffer, bufferByte, bufferByte.Length); string Str = ""; Str += Encoding.ASCII.GetString(bufferByte, 0, bufferByte.Length); Str = Str.Substring(0, Str.Length - 10); flagl = true; this.BeginInvoke(new System.Threading.ThreadStart(delegate() { fileLength = Str; webBrowser1.Visible = false; pictureBox1.Visible = false; label2.Visible = true; SendSucessPacket(); //回发数据接收成功标志位 bufferByte = null; this.toolStripStatusLabel1.Text = "接收文件长度完成"; this.toolStripProgressBar1.Maximum = int.Parse(fileLength); this.toolStripProgressBar1.Value = 0; this.toolStripStatusLabel3.Text = ""; })); SendSucessPacket(); Array.Clear(MsgBuffer, 0, MsgBuffer.Length); } else if (IsBlockOverArray(MsgBuffer, REnd) == true) { string URLStr = @"d:\temp\" + fileName; // 判断文件是否存在 如果存在则删除 if (cnt == 0) { FileInfo fileInf = new FileInfo(@"d:\temp\" + fileName); ProcessKILL("WINWORD"); ProcessKILL("EXCEL"); ProcessKILL("Foxit Reader"); ProcessKILL("AcroRd32"); this.BeginInvoke(new System.Threading.ThreadStart(delegate() { this.webBrowser1.Navigate("about:blank"); })); writer = new FileStream(URLStr, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); } if (fileLength != "" && fileName != "") { loadsecuss = true; bufferByte = new byte[REnd]; int received = REnd; receive = (long)received - 10 + receive; this.BeginInvoke(new System.Threading.ThreadStart(delegate() { if (receive <this.toolStripProgressBar1.Maximum) { this.toolStripProgressBar1.Value = int.Parse(receive.ToString()); this.toolStripStatusLabel2.Text = "接收到的文件长度:" + receive + "进度条的长度:" + this.toolStripProgressBar1.Value + "进度条最大值:" + this.toolStripProgressBar1.Maximum + "文件长度:" + fileLength; this.toolStripStatusLabel1.Text = "正在接收文件"; } else if (receive == this.toolStripProgressBar1.Maximum) { this.toolStripStatusLabel2.Text = "接收到的文件长度:" + receive + "进度条的长度:" + this.toolStripProgressBar1.Value + "进度条最大值:" + this.toolStripProgressBar1.Maximum + "文件长度:" + fileLength; this.toolStripStatusLabel3.Text = "接收文件完成"; //this.toolStripProgressBar1.Value = 0; } else { this.toolStripStatusLabel1.Text = "数据接收出错,请重新发送"; cnt = 0; receivingState = false; receive = 0; fileLength = ""; fileName = ""; extendLast = ""; writer.Close(); writer = null; this.toolStripProgressBar1.Value = 0; } })); writer.Write(MsgBuffer, 0, REnd - 10); writer.Flush(); SendSucessPacket(); cnt++; if (receive == int.Parse(fileLength)) { SendConfirmPacket(receive.ToString()); //接收文件并追尾 writer.Close(); writer = null; this.BeginInvoke(new System.Threading.ThreadStart(delegate() { string exfilename = URLStr.Substring(URLStr.Length - 3, 3); if (exfilename == "doc" || exfilename == "pdf") { this.pictureBox1.Visible = false; this.axWindowsMediaPlayer1.Visible = false; this.axWebBrowser1.Visible = false; this.webBrowser1.Visible = true; this.webBrowser1.Navigate(URLStr); } if (exfilename == "htm" || exfilename == "html") { this.pictureBox1.Visible = false; this.axWindowsMediaPlayer1.Visible = false; this.axWebBrowser1.Visible = false; this.webBrowser1.Visible = true; this.webBrowser1.Url = new Uri(URLStr); } if (exfilename == "jpg" || exfilename == "JPG" || exfilename == "bmp" || exfilename == "BMP") { this.axWindowsMediaPlayer1.Visible = false; this.webBrowser1.Visible = false; this.axWebBrowser1.Visible = false; this.pictureBox1.Visible = true; this.pictureBox1.Image = null; this.pictureBox1.Image = Image.FromFile(URLStr); } if (exfilename == "avi" || exfilename == "wmv" || exfilename == "rm" || exfilename == "rmvb" || exfilename == "flv") { this.webBrowser1.Visible = false; this.pictureBox1.Visible = false; this.axWebBrowser1.Visible = false; this.axWindowsMediaPlayer1.Visible = true; axWindowsMediaPlayer1.URL = URLStr; axWindowsMediaPlayer1.Ctlcontrols.play(); } if (exfilename == "xls") { try { this.webBrowser1.Visible = false; this.pictureBox1.Visible = false; this.axWebBrowser1.Visible = false; this.axWindowsMediaPlayer1.Visible = false; //对此实例进行验证,如果为null则表示运行此代码的机器可能未安装Excel Excel.ApplicationClass xlsApp = new ApplicationClass(); Excel.Application xls = new Excel.Application(); xlsApp.Visible = true; //打开现有的Excel文件 Workbook workbook = xlsApp.Workbooks.Open(URLStr, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); byte[] array = new byte[2]; //定义一组数组array array = System.Text.Encoding.ASCII.GetBytes(extendLast); //string转换的字母 int asciicode = (short)(array[0]); int asciicode1 = (short)(array[1]); if (asciicode >= 48 && asciicode <= 58 && asciicode1 >= 48 && asciicode1 <= 58) (xlsApp.ActiveWorkbook.Sheets[int.Parse(extendLast)] as Worksheet).Select(Type.Missing); //选中某个sheet页 } catch (Exception ex) { this.toolStripStatusLabel3.Text="文件名错误,请通知管理人员"; } }
3,打开指定的excel中sheet
View Code
Excel.ApplicationClass xlsApp = new ApplicationClass(); Excel.Application xls = new Excel.Application(); xlsApp.Visible = true; //打开现有的Excel文件 Workbook workbook = xlsApp.Workbooks.Open(URLStr, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); byte[] array = new byte[2]; //定义一组数组array array = System.Text.Encoding.ASCII.GetBytes(extendLast); //string转换的字母 int asciicode = (short)(array[0]); int asciicode1 = (short)(array[1]); if (asciicode >= 48 && asciicode <= 58 && asciicode1 >= 48 && asciicode1 <= 58) (xlsApp.ActiveWorkbook.Sheets[int.Parse(extendLast)] as Worksheet).Select(Type.Missing); //选中某个sheet页
如果有好的方法希望指出

浙公网安备 33010602011771号