1.using System;
2.using System.Collections.Generic;
3.using System.ComponentModel;
4.using System.Data;
5.using System.Drawing;
6.using System.Text;
7.using System.Windows.Forms;
8.
9.using System.Net;
10.using System.Threading;
11.using System.Net.Sockets;
12.
13.using System.IO;
14.
15.using System.Data.OracleClient;
16.using System.Data.OleDb;
17.
18.namespace OverSpeedMIS
19.{
20. public partial class FormSystemOnLineObject : Form
21. {
22. public FormSystemOnLineObject()
23. {
24. InitializeComponent();
25.
26. //不显示出dataGridView1的最后一行空白
27. dataGridView1.AllowUserToAddRows = false;
28. }
29.
30. /// <summary>
31. /// 作者:黑色头发
32. /// C# WinForm Socket传递多个文件并保存到oracle数据库的Blob字段
33. /// blog:http://heisetoufa.javaeye.com
34. /// 静网:http://www.heisetoufa.cn
35. /// </summary>
36.
37.
38. #region 定义变量
39.
40.
41. TcpListener lisner;
42. Thread TempThread;
43.
44. int xhMax = 0;//序号
45.
46. DBConnection dbc = new DBConnection();
47. OleDbConnection con;
48. OracleConnection connn;
49. OracleDataReader odrRepeat;
50.
51. #endregion
52.
53.
54. #region 进入窗体即启动服务
55.
56.
57. private void FormSystemOnLineObject_Load(object sender, EventArgs e)
58. {
59. //判断文件存不存在
60. if (!Directory.Exists(@txtFileSaveDir.Text))//若文件夹不存在则新建文件夹
61. {
62. Directory.CreateDirectory(@txtFileSaveDir.Text); //新建文件夹
63. }
64.
65. //开启接收线程
66. TempThread = new Thread(new ThreadStart(this.StartReceive));
67. TempThread.IsBackground = true;//设置为后台线程
68. TempThread.Start();
69. }
70.
71.
72.
73. private void StartReceive()
74. {
75. //创建一个网络端点
76. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse("2005"));
77.
78. //创建网络监听
79. lisner = new TcpListener(ipep);
80.
81. lisner.Start();
82.
83. while (true)
84. {
85.
86. ////确认连接
87. if (!lisner.Pending())
88. {
89.
90. Thread.Sleep(1000);
91. continue;
92. }
93.
94. //MessageBox.Show("1");
95.
96. Socket client = lisner.AcceptSocket();
97.
98. //获得客户端节点对象
99. IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
100.
101. //获得[文件名]
102. string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
103.
104. //获得[包的大小]
105. string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
106.
107. //获得[包的总数量]
108. int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
109.
110. //获得[最后一个包的大小]
111. string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
112.
113. //创建一个新文件
114. string fileFullName = txtFileSaveDir.Text + "\\" + SendFileName;
115.
116. FileStream MyFileStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.Read);
117.
118. //已发送包的个数
119. int SendedCount = 0;
120.
121. while (true)
122. {
123. byte[] data = TransferFiles.ReceiveVarData(client);
124. if (data.Length == 0)
125. {
126. break;
127. }
128. else
129. {
130. SendedCount++;
131. //将接收到的数据包写入到文件流对象
132. MyFileStream.Write(data, 0, data.Length);
133. }
134. }
135.
136.
137. //关闭文件流
138. MyFileStream.Close();
139.
140.
141. //关闭套接字
142. client.Close();
143. IPHostEntry ipHostEntry = Dns.GetHostEntry(IPAddress.Parse(clientep.Address.ToString()));
144. string clientHostName = ipHostEntry.HostName;
145.
146.
147. //················································
148. //保存接收的文件到数据库里
149. if (checkBox2.Checked == true)
150. {
151. string cnnstr = "provider=OraOLEDB.Oracle;data source=zlkj_kk;User Id=kk;Password=kk;";
152. con = new OleDbConnection(cnnstr);
153. try
154. {
155. con.Open();
156. }
157. catch
158. { }
159. OleDbCommand cmd = new OleDbCommand(cnnstr, con);
160.
161. cmd.CommandType = CommandType.Text;
162. cmd.CommandText = cnnstr;
163.
164. //string imgPath = @"d:\aa\a.jpg";//图片文件所在路径+文件名
165.
166. string imgPath = @fileFullName;//图片文件所在路径+文件名
167.
168. FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
169. Byte[] imgByte = new Byte[file.Length];//把图片转成 Byte型 二进制流
170. file.Read(imgByte, 0, imgByte.Length);//把二进制流读入缓冲区
171. file.Close();
172.
173.
174. //========================================================================================
175. //插入之前检查有有无重复数据
176. connn = dbc.getConnection();//获得conn连接
177.
178. try
179. {
180. connn.Open();
181.
182. OracleCommand cmdd = connn.CreateCommand();
183.
184. cmdd.CommandText = "SELECT max(cast( xh as int)) as xh FROM kk.kkcltj ";//插入之前检查有有无重复数据
185. odrRepeat = cmdd.ExecuteReader();//创建一个OracleDateReader对象
186. if (odrRepeat.Read())//读取数据,如果odr.Read()返回为true的话,就说明到登陆成功了
187. {
188. xhMax = Convert.ToInt32(odrRepeat["xh"].ToString());
189. }
190. }
191. catch (Exception ee)
192. {
193. MessageBox.Show(ee.Message.ToString());
194. }
195.
196. //========================================================================================
197.
198.
199. string strr = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1).ToString();//不带路径的文件名
200. //MessageBox.Show(" "+strr);
201.
202. DateTime dTime;
203. string kkdd = "";//卡口地点
204. string xsfx = "";//行驶方向
205.
206. string xscd = "";//行驶车道
207. int xzsd = 0;//限制速度
208. int sjsd = 0;//实际速度
209. string csbz = "";//超速标志
210. string hpzl = "";//号牌种类
211. string hphmm = "";//号牌号码
212.
213. if (fileFullName.Length > 42)
214. {
215. int yearFile = Convert.ToInt32(strr.Substring(0, 4));//截取年
216. int monthFile = Convert.ToInt32(strr.Substring(4, 2));//截取月
217. int dateFile = Convert.ToInt32(strr.Substring(6, 2));//截取日
218. int hourFile = Convert.ToInt32(strr.Substring(8, 2));//截取时
219. int minuteFile = Convert.ToInt32(strr.Substring(10, 2));//截取分
220. int secondFile = Convert.ToInt32(strr.Substring(12, 2));//截取秒
221. //int millisecondFile = Convert.ToInt32(strr.Substring(14, 3));//
222.
223. dTime = new DateTime(yearFile, monthFile, dateFile, hourFile, minuteFile, secondFile);
224. //MessageBox.Show(dt.ToString("yyyy-MM-dd HH-mm-ss fff"));
225.
226.
227.
228. kkdd = strr.Substring(14, 4);//卡口地点
229. xsfx = strr.Substring(18, 1); ;//行驶方向
230.
231. xscd = strr.Substring(19, 1);//行驶车道
232. xzsd = Convert.ToInt32(strr.Substring(20, 3));//限制速度
233. sjsd = Convert.ToInt32(strr.Substring(23, 3));//实际速度
234. csbz = strr.Substring(26, 1);//超速标志
235. hpzl = strr.Substring(27, 2);//号牌种类
236. hphmm = strr.Substring(29, 7);//号牌号码
237. //照片大小
238.
239.
240. //—————————————————————————————————————————————
241. //把信息插入数据库
242.
243. xhMax++;
244.
245. //MessageBox.Show(xhMax.ToString());
246. 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语句插入数据库
247.
248. //MessageBox.Show(cmd.CommandText.ToString());
249.
250.
251. cmd.Parameters.Add("zp", System.Data.OleDb.OleDbType.Binary, imgByte.Length);
252. cmd.Parameters[0].Value = imgByte;
253.
254. try
255. {
256. cmd.ExecuteNonQuery();
257. //MessageBox.Show("插入成功");
258. }
259. catch (System.Exception e1)
260. {
261. MessageBox.Show(e1.Message);
262. }
263.
264. //—————————————————————————————————————————————
265. }
266. else
267. {
268.
269. }
270. }
271. else if (checkBox1.Checked == true)
272. {
273. //AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, "连接类型占位", "关联对象占位");
274. }
275. else if (checkBox1.Checked == false && checkBox2.Checked == false)
276. {
277. MessageBox.Show("选择保存到硬盘还是数据库");
278. break;
279. }
280.
281. //················································
282. con.Close();
283. connn.Close();
284. odrRepeat.Close();
285.
286. //填加到dgv里
287. //文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小
288. //AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, bagLast);
289. AddRow(clientHostName, "说明占位", clientep.Address, SendedCount, SendFileName, bagCount, "连接类型占位", "关联对象占位");
290. }
291. }
292.
293.
294. private delegate void DelAddRow(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8);
295.
296. private void AddRow(object o1, object o2, object o3, object o4, object o5, object o6, object o7, object o8)
297. {
298. if (InvokeRequired)
299. {
300. DelAddRow dar = new DelAddRow(AddRow);
301. this.Invoke(dar, o1, o2, o3, o4, o5, o6, o7, o8);
302. return;
303. }
304. this.dataGridView1.Rows.Add(o1, o2, o3, o4, o5, o6, o7, o8);
305. }
306.
307.
308.
309. #endregion
310.
311.
312. #region 拦截Windows消息,关闭窗体时执行
313. protected override void WndProc(ref Message m)
314. {
315. const int WM_SYSCOMMAND = 0x0112;
316. const int SC_CLOSE = 0xF060;
317. if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
318. {
319. //捕捉关闭窗体消息
320. // User clicked close button
321. //this.WindowState = FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化
322.
323. TempThread.Abort();
324. lisner.Stop();
325.
326.
327. ServiceStop();
328. }
329. base.WndProc(ref m);
330. }
331. #endregion
332.
333.
334. #region 停止服务
335.
336. //停止服务
337. private void ServiceStop()
338. {
339. try
340. {
341.
342. }
343. catch { }
344.
345. try
346. {
347.
348. }
349. catch { }
350. }
351.
352. #endregion
353.
354. private void button1_Click(object sender, EventArgs e)
355. {
356. if (fbdFileSave.ShowDialog() == DialogResult.OK)
357. {
358. txtFileSaveDir.Text = fbdFileSave.SelectedPath;
359. }
360. }
361.
362. private void toolStripStatusLabel1_Click(object sender, EventArgs e)
363. {
364.
365. }
366.
367. private void toolStripStatusLabel2_Click(object sender, EventArgs e)
368. {
369. System.Diagnostics.Process.Start("http://heisetoufa.javaeye.com");
370. }
371. }
372.}
客户发送端代码省略,详见http://heisetoufa.javaeye.com/blog/257070或下载源码
转别人的