今天把一个功能模块做成了http服务,这还是第一次写http服务,纪录下来。
1 package com.chuntent.itemsearch; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStream; 8 import java.net.InetSocketAddress; 9 import java.net.URI; 10 import java.net.URLDecoder; 11 12 import net.sf.json.JSONArray; 13 import net.sf.json.JSONObject; 14 15 import com.chuntent.tool.StringTool; 16 import com.sun.net.httpserver.HttpExchange; 17 import com.sun.net.httpserver.HttpHandler; 18 import com.sun.net.httpserver.HttpServer; 19 import com.sun.net.httpserver.spi.HttpServerProvider; 20 21 public class SearchHttpServer { 22 public static SearchEngine engine = new SearchEngine(); 23 //启动服务,监听来自客户端的请求 24 public static void httpserverService() throws IOException { 25 engine.initFromSQL(); 26 HttpServerProvider provider = HttpServerProvider.provider(); 27 HttpServer httpserver =provider.createHttpServer(new InetSocketAddress(8089), 100);//监听端口6666,能同时接 受100个请求 28 httpserver.createContext("/searchserver", new MyHttpHandler()); 29 httpserver.setExecutor(null); 30 httpserver.start(); 31 System.out.println("server started"); 32 } 33 //Http请求处理类 34 static class MyHttpHandler implements HttpHandler { 35 public void handle(HttpExchange httpExchange) throws IOException { 36 String method = httpExchange.getRequestMethod(); 37 //响应信息 38 String responseMsg = "fail"; 39 //传入参数 40 StringBuilder sb = new StringBuilder(); 41 //get方法 42 if (method.equals("GET")) { 43 URI uri = httpExchange.getRequestURI(); 44 sb.append(uri.getQuery()); 45 } else if (method.equals("POST")){ 46 47 InputStream in = httpExchange.getRequestBody(); // 获得输入流 48 BufferedReader reader = new BufferedReader( 49 new InputStreamReader(in)); 50 String temp = null; 51 while ((temp = reader.readLine()) != null) { 52 sb.append(temp); 53 } 54 } 55 else { 56 57 } 58 String [] para = StringTool.split(URLDecoder.decode(sb.toString() , "utf-8"), "&", false); 59 String query = ""; 60 int type = -1; 61 int sortMethod = 1; 62 int pageIndex = 1; 63 int pageSize = 32; 64 double creditMin ,creditMax , priceMin ,priceMax ; 65 creditMin = creditMax = priceMin = priceMax = -1 ; 66 boolean suc = true; 67 for(String line : para){ 68 String [] array = line.split("="); 69 if(array.length < 2) 70 suc = false; 71 if(array[0].equals("query")) 72 query = array[1]; 73 else if(array[0].equals("type")) 74 type = Integer.parseInt(array[1]); 75 else if(array[0].equals("sortMethod")) 76 sortMethod = Integer.parseInt(array[1]); 77 else if(array[0].equals("creditMin")) 78 creditMin = Double.parseDouble(array[1]); 79 else if(array[0].equals("creditMax")) 80 creditMax = Double.parseDouble(array[1]); 81 else if(array[0].equals("priceMin")) 82 priceMin = Double.parseDouble(array[1]); 83 else if(array[0].equals("priceMax")) 84 priceMax = Double.parseDouble(array[1]); 85 else if(array[0].equals("pageIndex")) 86 pageIndex = Integer.parseInt(array[1]); 87 else if(array[0].equals("pageSize")) 88 pageSize = Integer.parseInt(array[1]); 89 90 } 91 if(suc){ 92 long current = System.currentTimeMillis(); 93 //creditMin, double creditMax, double priceMin, double priceMax 94 String result = engine.search(query, sortMethod , type , creditMin ,creditMax , priceMin ,priceMax , pageIndex , pageSize); 95 long duration = System.currentTimeMillis() - current; 96 97 JSONObject jobj = JSONObject.fromObject(result); 98 jobj.put("time", duration); 99 responseMsg = jobj.toString(); 100 } 101 else{ 102 System.out.println("para error !"); 103 } 104 httpExchange.sendResponseHeaders(200, responseMsg.getBytes().length); //设置响应头属性及响应信息的长度 105 OutputStream out = httpExchange.getResponseBody(); //获得输出流 106 out.write(responseMsg.getBytes()); 107 out.flush(); 108 httpExchange.close(); 109 110 } 111 } 112 public static void main(String[] args) throws IOException { 113 httpserverService(); 114 } 115 }