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

Java实现轻量型Web服务器接收http协议提交的RFID读卡信息_第1张图片

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class httpserver {
    public static void main(String[] args) throws Exception{
        int listenport=80;                                         /*监听端口号*/
        if (args[0]!=null){listenport=Integer.parseInt(args[0]);}  //侦听自定义的端口

        try {
            ServerSocket ss = new ServerSocket(listenport);

            while (true) {
                String condition="";
                String ResponseStr="";

                Socket socket = ss.accept();                /*实例化客户端,固定套路,通过服务端接受的对象,生成相应的客户端实例*/
                BufferedReader bd = new BufferedReader(new InputStreamReader(socket.getInputStream()));     /*获取客户端输入流,就是请求过来的基本信息:请求头,换行符,请求体*/

                String requestHeader;
                int contentLength = 0;
                while ((requestHeader = bd.readLine()) != null && !requestHeader.isEmpty()) {   /*** 接受HTTP请求,并解析数据 */
                    System.out.println(requestHeader);

                    /*** 获得GET参数*/
                    if (requestHeader.startsWith("GET")) {
                        int begin = requestHeader.indexOf("?") + 1;
                        int end = requestHeader.indexOf("HTTP/");
                        condition = requestHeader.substring(begin, end);
                        ResponseStr=AnalyticHttpInfo(condition);
                    }
                    else {
                        /*** 获得POST参数* 1.获取请求内容长度*/
                        if (requestHeader.startsWith("Content-Length")) {
                            int begin = requestHeader.indexOf("Content-Lengh:") + "Content-Length:".length();
                            String postParamterLength = requestHeader.substring(begin + 1).trim();
                            contentLength = Integer.parseInt(postParamterLength);
                        }
                    }
                }

                StringBuffer sb = new StringBuffer();
                if (contentLength > 0) {
                    for (int i = 0; i < contentLength; i++) {
                        sb.append((char) bd.read());
                    }
                    System.out.println("POST parameter: " + sb.toString());
                    condition=sb.toString().replace("\r\n","");
                    condition=condition.replace("{","");        //可以引用JSON的Jar包来解析json数据,这里将它转一般字符串来处理
                    condition=condition.replace("}","");
                    condition=condition.replace(",","&");
                    condition=condition.replace(":","=");
                    condition=condition.replace("\"","");
                    ResponseStr=AnalyticHttpInfo(condition);      //解析读卡器上传的包序号、卡号、机号等信息,并生成回应字符串
                }

                /*发送回应信息*/
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                pw.println(ResponseStr);
                pw.flush();
                System.out.println("Response: "+ResponseStr+"\r\n");
            }
        } catch (IOException e) {
             e.printStackTrace();
        }
    }

    /*-解析读卡器上传的包序号、机号、卡类型、卡号、卡内数据、设备序列号、读卡状态 等信息,并生成回应字符串------------------------------*/
    static String AnalyticHttpInfo(String inputstr) throws Exception{
        String info="";
        String jihao="";
        String cardtype="";
        String card="";
        String data="";
        String dn="";
        String status="";

        String[] strArr = inputstr.split("&");
        for (int i=0;i

你可能感兴趣的:(网络读卡器,18002295132,QQ:954486673,java,http,前端,打菲,Http,RFID)