http 服务

时间:2024-08-05 11:06:26

今天把一个功能模块做成了http服务,这还是第一次写http服务,纪录下来。

 package com.chuntent.itemsearch;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import com.chuntent.tool.StringTool;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider; public class SearchHttpServer {
public static SearchEngine engine = new SearchEngine();
//启动服务,监听来自客户端的请求
public static void httpserverService() throws IOException {
engine.initFromSQL();
HttpServerProvider provider = HttpServerProvider.provider();
HttpServer httpserver =provider.createHttpServer(new InetSocketAddress(8089), 100);//监听端口6666,能同时接 受100个请求
httpserver.createContext("/searchserver", new MyHttpHandler());
httpserver.setExecutor(null);
httpserver.start();
System.out.println("server started");
}
//Http请求处理类
static class MyHttpHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
String method = httpExchange.getRequestMethod();
//响应信息
String responseMsg = "fail";
//传入参数
StringBuilder sb = new StringBuilder();
//get方法
if (method.equals("GET")) {
URI uri = httpExchange.getRequestURI();
sb.append(uri.getQuery());
} else if (method.equals("POST")){ InputStream in = httpExchange.getRequestBody(); // 获得输入流
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String temp = null;
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
}
else { }
String [] para = StringTool.split(URLDecoder.decode(sb.toString() , "utf-8"), "&", false);
String query = "";
int type = -1;
int sortMethod = 1;
int pageIndex = 1;
int pageSize = 32;
double creditMin ,creditMax , priceMin ,priceMax ;
creditMin = creditMax = priceMin = priceMax = -1 ;
boolean suc = true;
for(String line : para){
String [] array = line.split("=");
if(array.length < 2)
suc = false;
if(array[0].equals("query"))
query = array[1];
else if(array[0].equals("type"))
type = Integer.parseInt(array[1]);
else if(array[0].equals("sortMethod"))
sortMethod = Integer.parseInt(array[1]);
else if(array[0].equals("creditMin"))
creditMin = Double.parseDouble(array[1]);
else if(array[0].equals("creditMax"))
creditMax = Double.parseDouble(array[1]);
else if(array[0].equals("priceMin"))
priceMin = Double.parseDouble(array[1]);
else if(array[0].equals("priceMax"))
priceMax = Double.parseDouble(array[1]);
else if(array[0].equals("pageIndex"))
pageIndex = Integer.parseInt(array[1]);
else if(array[0].equals("pageSize"))
pageSize = Integer.parseInt(array[1]); }
if(suc){
long current = System.currentTimeMillis();
//creditMin, double creditMax, double priceMin, double priceMax
String result = engine.search(query, sortMethod , type , creditMin ,creditMax , priceMin ,priceMax , pageIndex , pageSize);
long duration = System.currentTimeMillis() - current; JSONObject jobj = JSONObject.fromObject(result);
jobj.put("time", duration);
responseMsg = jobj.toString();
}
else{
System.out.println("para error !");
}
httpExchange.sendResponseHeaders(200, responseMsg.getBytes().length); //设置响应头属性及响应信息的长度
OutputStream out = httpExchange.getResponseBody(); //获得输出流
out.write(responseMsg.getBytes());
out.flush();
httpExchange.close(); }
}
public static void main(String[] args) throws IOException {
httpserverService();
}
}