当前位置:   article > 正文

Java实现轻量型Web服务器接收http协议提交的RFID读卡信息_java读取rfid数据

java读取rfid数据

  示例使用的读卡器:RFID网络WIFI无线TCP/UDP/HTTP可编程二次开发读卡器POE供电语音-淘宝网 (taobao.com)

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. public class HttpServer_Java {
  9. //监听端口
  10. private static final int PORT = 88;
  11. public static void main(String[] args) throws IOException {
  12. ServerSocket serverSocket = null;
  13. Socket socket = null;
  14. try {
  15. //建立服务器的Socket,并设定一个监听的端口PORT
  16. serverSocket = new ServerSocket(PORT);
  17. //由于需要进行循环监听,因此获取消息的操作应放在一个while大循环中
  18. while(true){
  19. try {
  20. //建立跟客户端的连接
  21. socket = serverSocket.accept();
  22. ServerThread thread = new ServerThread(socket);
  23. thread.start();
  24. } catch (Exception e) {
  25. System.out.println("建立与客户端的连接出现异常");
  26. e.printStackTrace();
  27. }
  28. //ServerThread thread = new ServerThread(socket);
  29. //thread.start();
  30. }
  31. } catch (Exception e) {
  32. System.out.println("端口被占用");
  33. e.printStackTrace();
  34. }
  35. finally {
  36. serverSocket.close();
  37. }
  38. }
  39. }
  40. //服务端线程类
  41. //继承Thread类的话,必须重写run方法,在run方法中定义需要执行的任务。
  42. class ServerThread extends Thread {
  43. private Socket socket ;
  44. InputStream inputStream;
  45. OutputStream outputStream;
  46. public ServerThread(Socket socket){
  47. this.socket=socket;
  48. }
  49. public void run(){
  50. try {
  51. while (true){
  52. //接收客户端的消息,解析信息并回应读卡器
  53. //System.out.println(socket);
  54. byte[] bytes =new byte[10240];
  55. inputStream =socket.getInputStream();
  56. int GetDataLen=0;
  57. while ((GetDataLen=inputStream.read(bytes))!=-1){ //通过这个方法读取全部数据 及 长度,
  58. break;
  59. }
  60. if(GetDataLen>0) {
  61. String condition="";
  62. String requestHeader= new String(bytes);
  63. Date date=new Date();
  64. SimpleDateFormat formatter=new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS");
  65. String TimeStr=formatter.format(date);
  66. System.out.println(TimeStr+" Received from " + socket.getRemoteSocketAddress() + " : " + requestHeader);
  67. /*** 获得GET参数*/
  68. if (requestHeader.startsWith("GET")) {
  69. int begin = requestHeader.indexOf("?") + 1;
  70. int end = requestHeader.indexOf("HTTP/");
  71. condition = requestHeader.substring(begin, end);
  72. }
  73. else {
  74. /*** 获得POST参数* 1.获取请求内容长度*/
  75. if (requestHeader.startsWith("POST")) {
  76. int begin = requestHeader.lastIndexOf("info") ;
  77. condition = requestHeader.substring(begin).trim();
  78. }
  79. }
  80. condition=condition.replace("{",""); //可以引用JSON的Jar包来解析json数据,这里将它转一般字符串来处理
  81. condition=condition.replace("}","");
  82. condition=condition.replace(",","&");
  83. condition=condition.replace(":","=");
  84. condition=condition.replace("\"","");
  85. String ResponseStr=AnalyticHttpInfo(condition); //解析读卡器上传的包序号、卡号、机号等信息,并生成回应字符串
  86. byte[] RespByte = ResponseStr.getBytes("gb2312");
  87. outputStream = socket.getOutputStream();
  88. outputStream.write(RespByte);
  89. System.out.println(ResponseStr+'\n');
  90. }
  91. }
  92. } catch (Exception e) {
  93. System.out.println("客户端主动断开连接了");
  94. e.printStackTrace();
  95. }
  96. //操作结束,关闭socket
  97. try{
  98. socket.close();
  99. }catch(IOException e){
  100. System.out.println("关闭连接出现异常");
  101. e.printStackTrace();
  102. }
  103. }
  104. /*-解析读卡器上传的包序号、机号、卡类型、卡号、卡内数据、设备序列号、读卡状态 等信息,并生成回应字符串------------------------------*/
  105. static String AnalyticHttpInfo(String inputstr) throws Exception {
  106. String info = "";
  107. String jihao = "";
  108. String cardtype = "";
  109. String card = "";
  110. String data = "";
  111. String dn = "";
  112. String status = "";
  113. String heartbeattype = "";
  114. String input = "";
  115. String output = "";
  116. String time = "";
  117. String rand = "";
  118. int typenum;
  119. int pushortake = 0;
  120. String[] strArr = inputstr.split("&");
  121. for (int i = 0; i < strArr.length; i++) {
  122. String[] strPara = strArr[i].split("=");
  123. switch (strPara[0]) {
  124. case "info":
  125. info = strPara[1]; //接收到的数据包号,回应时要带入该包号才能正确回应
  126. break;
  127. case "jihao":
  128. jihao = strPara[1]; //设备机号(可自编)
  129. break;
  130. case "cardtype":
  131. cardtype = strPara[1];
  132. typenum = (Integer.parseInt(cardtype, 16)) % 16; //卡类型,typenum=1为ID卡,2为HID卡,3为T5557卡,4为EM4305卡,5为IC卡,6为二代证,7为15693卡,8为hid iclass卡
  133. pushortake = (Integer.parseInt(cardtype, 16)) / 128; //pushortake=0 表示读卡,>0表示卡离开感应区
  134. break;
  135. case "card":
  136. card = strPara[1].trim(); //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
  137. break;
  138. case "data":
  139. data = strPara[1]; //读取卡片扇区内容
  140. break;
  141. case "dn":
  142. dn = strPara[1].trim(); //设备硬件序列号,出厂时已固化,全球唯一
  143. break;
  144. case "status":
  145. status = strPara[1]; //读卡状态,如密码认证失败为12
  146. break;
  147. case "heartbeattype":
  148. heartbeattype = strPara[1]; //心跳包标志
  149. break;
  150. case "input":
  151. input = strPara[1]; //输入接口状态
  152. break;
  153. case "output":
  154. output = strPara[1]; //输出接口状态
  155. break;
  156. case "time":
  157. time = strPara[1]; //设备时钟
  158. break;
  159. case "rand":
  160. rand = strPara[1]; //随机数
  161. break;
  162. }
  163. }
  164. if (heartbeattype.equals("1") && dn.length() == 16 && info.length() > 0) { //接收到心跳包
  165. //return "Response=1," + info + ",,0,0," ;
  166. String DisplayStr = dn + GetChineseCode("接收到心跳信息! "); //正式项目可以用不显示文字、不响声、不播报语音的指令来回应心跳,此处加入显示、响声只是用来检测读卡器功能
  167. return "Response=1," + info + "," + DisplayStr + ",20,2,";
  168. } else if (dn.length() == 16 && card.length() > 4 && info.length() > 0) { //接收到刷卡信息
  169. String DisplayStr = "{" + GetChineseCode("卡号") + ":}"; //发送到读卡器上显示的文字,中文要转换编码,英文、数字可以不需要转换,{}中的文字可以高亮显示
  170. DisplayStr = DisplayStr + (card + " ").substring(0, 12) + GetSysDT();
  171. String ChineseVoice = "[v1]"; //[v1] 表示本次播报的语音量,取值范围 v1 到 v16
  172. if (pushortake > 0) {
  173. ChineseVoice = ChineseVoice + GetChineseCode("卡号") + "[n1]" + card + GetChineseCode("离开感应区!"); //发送到读卡器上播报的TTS中文语音,所有中文要转换编码,英文、数字可以不需要转换,[n1]表示数字播方方式
  174. } else {
  175. ChineseVoice = ChineseVoice + GetChineseCode("读取卡号") + "[n1]" + card;
  176. }
  177. //Response=1 是指定的回应头信息+接收到的包序号+发送到读卡器的显示文字,+显示延时秒数+蜂鸣响声代码+播报的TTS语音
  178. return "Response=1," + info + "," + DisplayStr + ",20,2," + ChineseVoice;
  179. }else{
  180. return "Response=1,"; //其他未知的消息
  181. }
  182. }
  183. /*取电脑系统日期时间-------------------------------------------------------------------------------------------------*/
  184. static String GetSysDT() {
  185. Date date=new Date();
  186. SimpleDateFormat formatter=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
  187. String TimeStr=formatter.format(date);
  188. return TimeStr;
  189. }
  190. /*获取中文编码信息----------------------------------------------------------------------------------------------*/
  191. static String GetChineseCode(String inputstr) throws Exception{
  192. int strlen=inputstr.length();
  193. String hexcode="";
  194. for (int j=0;j<strlen;j++) {
  195. String str=inputstr.substring(j,j+1);
  196. byte[] Chinesecodearry = str.getBytes("gb2312");
  197. int codelen=Chinesecodearry.length;
  198. if(codelen==1){
  199. hexcode=hexcode+str;
  200. }else{
  201. hexcode=hexcode+"\\x";
  202. for (int i=0;i<codelen;i++) {
  203. String bytestr="00"+Integer.toHexString(Chinesecodearry[i] & 0xff);
  204. hexcode=hexcode+ bytestr.substring(bytestr.length() -2,bytestr.length());
  205. }
  206. }
  207. }
  208. return hexcode;
  209. }
  210. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/990625
推荐阅读
相关标签
  

闽ICP备14008679号