请求信息十六进制显示
package web; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyHttpServer { private static int count = 1; private static char arr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; public static void main(String[] args) throws IOException { byte b = 0x3f; byte2(b); ServerSocket server = new ServerSocket(8080); System.out.println("服务器已经启动,监听端口在8080..."); while (true) { Socket socket = server.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); byte[] buf = new byte[1024*1024]; new Thread(new Runnable() { @Override public void run() { int len = 0; try { len = is.read(buf); if (len == -1) return; System.out.println("读到的字节数量:" + len); String s1 = bytes2(buf, len); System.out.println(s1); String s = new String(buf, 0, len); System.out.println(s); os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content-Type: text/plain\r\n".getBytes()); os.write("\r\n".getBytes()); os.write((" "+count).getBytes()); count++; os.flush(); } catch (IOException e) { throw new RuntimeException(e); }finally { if (socket != null){ try { socket.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }).start(); } } private static String bytes2(byte[] buf,int len){ StringBuffer sBuffer = new StringBuffer(); int cnt = 0; for(int i=0;i<len;i++){ sBuffer.append(byte2(buf[i]) + " "); cnt++; if (cnt % 8 == 0) sBuffer.append(" "); if (cnt % 16 == 0) sBuffer.append("\r\n"); } return sBuffer.toString(); } private static String byte2(byte bt){ int lo = bt & 0b00001111; int hi = (bt & 0b11110000) >> 4;//位移运算符 移四位,得到四个1 char clo = arr[lo]; char chi = arr[hi]; return chi+ "" + clo; } }
访问页面
请求信息AXCLL码显示
package web; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyHttpServer { private static int count = 1; private static char arr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; public static void main(String[] args) throws IOException { byte b = 0x3f; byte2(b); ServerSocket server = new ServerSocket(8080); System.out.println("服务器已经启动,监听端口在8080..."); while (true) { Socket socket = server.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); byte[] buf = new byte[1024*1024]; new Thread(new Runnable() { @Override public void run() { int len = 0; try { len = is.read(buf); if (len == -1) return; System.out.println("读到的字节数量:" + len); String s1 = bytes2(buf, len); System.out.println(s1); String s = new String(buf, 0, len); System.out.println(s); os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content-Type: text/plain\r\n".getBytes()); os.write("\r\n".getBytes()); os.write((" "+count).getBytes()); count++; os.flush(); } catch (IOException e) { throw new RuntimeException(e); }finally { if (socket != null){ try { socket.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }).start(); } } private static String bytes2(byte[] buf,int len){ StringBuffer sBuffer = new StringBuffer(); StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); int cnt = 0; for(int i=0;i<len;i++){ sb1.append(byte2(buf[i]) + " "); if (buf[i] >= 0x20 && buf[i] <= 0x7e){ sb2.append((char)buf[i]); }else { sb2.append("."); } cnt++; if (cnt % 8 == 0) sb1.append(" "); if (cnt % 16 == 0) { sBuffer.append(sb1).append(sb2).append("\r\n"); sb1 = new StringBuffer(); sb2 = new StringBuffer(); cnt = 0; } } if (cnt != 0) sBuffer.append(sb1).append(" ").append(sb2).append("\r\n"); return sBuffer.toString(); } private static String byte2(byte bt){ int lo = bt & 0b00001111; int hi = (bt & 0b11110000) >> 4;//位移运算符 移四位,得到四个1 char clo = arr[lo]; char chi = arr[hi]; return chi+ "" + clo; } }
访问页面
差不多一样即可
标签:十六进制,请求,AXCLL,buf,len,import,new,os,String From: https://www.cnblogs.com/yu3304/p/17275837.html