using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Threading; using System.Net.Sockets; using System.IO; using System.Data.OracleClient; using System.Data.OleDb; namespace OverSpeedMIS { public partial class FormSystemOnLineObject : Form { public FormSystemOnLineObject() { InitializeComponent(); //不显示出dataGridView1的最后一行空白 dataGridView1.AllowUserToAddRows = false; } /// <summary> /// 作者:黑色头发 /// C# WinForm Socket传递多个文件并保存到oracle数据库的Blob字段 /// blog:http://heisetoufa.iteye.com /// 静网:http://www.heisetoufa.cn /// </summary> #region 定义变量 TcpListener lisner; Thread TempThread; int xhMax = 0;//序号 DBConnection dbc = new DBConnection(); OleDbConnection con; OracleConnection connn; OracleDataReader odrRepeat; #endregion #region 进入窗体即启动服务 private void FormSystemOnLineObject_Load(object sender, EventArgs e) { //判断文件存不存在 if (!Directory.Exists(@txtFileSaveDir.Text))//若文件夹不存在则新建文件夹 { Directory.CreateDirectory(@txtFileSaveDir.Text); //新建文件夹 } //开启接收线程 TempThread = new Thread(new ThreadStart(this.StartReceive)); TempThread.IsBackground = true;//设置为后台线程 TempThread.Start(); } private void StartReceive() { //创建一个网络端点 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse("2005")); //创建网络监听 lisner = new TcpListener(ipep); lisner.Start(); while (true) { ////确认连接 if (!lisner.Pending()) { Thread.Sleep(1000); continue; } //MessageBox.Show("1"); Socket client = lisner.AcceptSocket(); //获得客户端节点对象 IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; //获得[文件名] string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //获得[包的大小] string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //获得[包的总数量] int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client))); //获得[最后一个包的大小] string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)); //创建一个新文件 string fileFullName = txtFileSaveDir.Text + "\\" + SendFileName; FileStream MyFileStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.Read); //已发送包的个数 int SendedCount = 0; while (true) { byte[] data = TransferFiles.ReceiveVarData(client); if (data.Length == 0) { break; } else { SendedCount++; //将接收到的数据包写入到文件流对象 MyFileStream.Write(data, 0, data.Length); } } //关闭文件流 MyFileStream.Close(); //关闭套接字 client.Close(); IPHostEntry ipHostEntry = Dns.GetHostEntry(IPAddress.Parse(clientep.Address.ToString())); string clientHostName = ipHostEntry.HostName; //················································ //保存接收的文件到数据库里 if (checkBox2.Checked == true) { string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;"; con = new OleDbConnection(cnnstr); try { con.Open(); } catch { } OleDbCommand cmd = new OleDbCommand(cnnstr, con); cmd.CommandType = CommandType.Text; cmd.CommandText = cnnstr; //string imgPath = @"d:\aa\a.jpg";//图片文件所在路径+文件名 string imgPath = @fileFullName;//图片文件所在路径+文件名 FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read); Byte[] imgByte = new Byte[file.Length];//把图片转成 Byte型 二进制流 file.Read(imgByte, 0, imgByte.Length);//把二进制流读入缓冲区 file.Close(); //======================================================================================== //插入之前检查有有无重复数据 connn = dbc.getConnection();//获得conn连接 try { connn.Open(); OracleCommand cmdd = connn.CreateCommand(); cmdd.CommandText = "SELECT max(cast( xh as int)) as xh FROM kk.kkcltj ";//插入之前检查有有无重复数据 odrRepeat = cmdd.ExecuteReader();//创建一个OracleDateReader对象 if (odrRepeat.Read())//读取数据,如果odr.Read()返回为true的话,就说明到登陆成功了 { xhMax = Convert.ToInt32(odrRepeat["xh"].ToString()); } } catch (Exception ee) { MessageBox.Show(ee.Message.ToString()); } //======================================================================================== string strr = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1).ToString();//不带路径的文件名 //MessageBox.Show(" "+strr); DateTime dTime; string kkdd = "";//卡口地点 string xsfx = "";//行驶方向 string xscd = "";//行驶车道 int xzsd = 0;//限制速度 int sjsd = 0;//实际速度 string csbz = "";//超速标志 string hpzl = "";//号牌种类 string hphmm = "";//号牌号码 if (fileFullName.Length > 42) { int yearFile = Convert.ToInt32(strr.Substring(0, 4));//截取年 int monthFile = Convert.ToInt32(strr.Substring(4, 2));//截取月 int dateFile = Convert.ToInt32(strr.Substring(6, 2));//截取日 int hourFile = Convert.ToInt32(strr.Substring(8, 2));//截取时 int minuteFile = Convert.ToInt32(strr.Substring(10, 2));//截取分 int secondFile = Convert.ToInt32(strr.Substring(12, 2));//截取秒 //int millisecondFile = Convert.ToInt32(strr.Substring(14, 3));// dTime = new DateTime(yearFile, monthFile, dateFile, hourFile, minuteFile, secondFile); //MessageBox.Show(dt.ToString("yyyy-MM-dd HH-mm-ss fff")); kkdd = strr.Substring(14, 4);//卡口地点 xsfx = strr.Substring(18, 1); ;//行驶方向 xscd = strr.Substring(19, 1);//行驶车道 xzsd = Convert.ToInt32(strr.Substring(20, 3));//限制速度 sjsd = Convert.ToInt32(strr.Substring(23, 3));//实际速度 csbz = strr.Substring(26, 1);//超速标志 hpzl = strr.Substring(27, 2);//号牌种类 hphmm = strr.Substring(29, 7);//号牌号码 //照片大小 //————————————————————————————————————————————— //把信息插入数据库 xhMax++; //MessageBox.Show(xhMax.ToString()); cmd.CommandText = "insert into kk.kkcltj(xh,zpsj,kkdd,xsfx,xscd,xzsd,sjsd,csbz,hphm,hpzl,zpdx,zp) values('" + xhMax.ToString() + "',to_date('" + dTime.ToString() + "','YYYY-MM-DD HH24:MI:SS'),'" + kkdd + "','" + xsfx + "' ,'" + xscd + "', " + xzsd + ", " + sjsd + ", '" + csbz + "','" + hphmm + "','" + hpzl + "','" + imgByte.Length.ToString() + "',:zp ) ";//正常sql语句插入数据库 //MessageBox.Show(cmd.CommandText.ToString()); cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, imgByte.Length); cmd.Parameters[0].Value = imgByte; try { cmd.ExecuteNonQuery(); //MessageBox.Show("插入成功"); } catch (System.Exception e1) { MessageBox.Show(e1.Message); } //————————————————————————————————————————————— } else { } } else if (checkBox1.Checked == true) { //AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, "连接类型占位", "关联对象占位"); } else if (checkBox1.Checked == false && checkBox2.Checked == false) { MessageBox.Show("选择保存到硬盘还是数据库"); break; } //················································ con.Close(); connn.Close(); odrRepeat.Close(); //填加到dgv里 //文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小 //AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, bagLast); AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, "连接类型占位", "关联对象占位"); } } private delegate void DelAddRow(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8); private void AddRow(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8) { if (InvokeRequired) { DelAddRow dar = new DelAddRow(AddRow); this.Invoke(dar, o1, o2, o3, o4, o5, o6, o7, o8); return; } this.dataGridView1.Rows.Add(o1, o2, o3, o4, o5, o6, o7, o8); } #endregion #region 拦截Windows消息,关闭窗体时执行 protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE) { //捕捉关闭窗体消息 // User clicked close button //this.WindowState = FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化 TempThread.Abort(); lisner.Stop(); ServiceStop(); } base.WndProc(ref m); } #endregion #region 停止服务 //停止服务 private void ServiceStop() { try { } catch { } try { } catch { } } #endregion private void button1_Click(object sender, EventArgs e) { if (fbdFileSave.ShowDialog() == DialogResult.OK) { txtFileSaveDir.Text = fbdFileSave.SelectedPath; } } private void toolStripStatusLabel1_Click(object sender, EventArgs e) { } private void toolStripStatusLabel2_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http://heisetoufa.iteye.com"); } } }