首页 > 其他 > 详细

http 服务

时间:2014-05-07 21:15:52      阅读:620      评论:0      收藏:0      [点我收藏+]

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.chuntent.keywords;
 
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.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
 
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;
import com.xinjian.tool.common.bean.Word;
import com.xinjian.tool.common.util.SplitTool;
 
public class MyHttpServer {
    //启动服务,监听来自客户端的请求
    public static void httpserverService() throws IOException {
        HttpServerProvider provider = HttpServerProvider.provider();
        HttpServer httpserver =provider.createHttpServer(new InetSocketAddress(8088), 100);//监听端口6666,能同时接 受100个请求
        httpserver.createContext("/getKeyWords", 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 responseMsg = "fail";   //响应信息
            InputStream in = httpExchange.getRequestBody(); //获得输入流        
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String temp = null;
            while((temp = reader.readLine()) != null) {
                sb.append(temp);
            }      
            String [] para = StringTool.split(URLDecoder.decode(sb.toString() , "utf-8"), "&", false);         
            String title = "";
            String body = "";
            boolean suc = true;
            for(String line : para){               
                String [] array = line.split("=");
                if(array.length < 2)
                    suc = false;
                if(array[0].equals("title"))
                    title = array[1];
                if(array[0].equals("body"))
                    body = array[1];
            }  
             
            if(suc){           
                ArrayList<Word> words = new KeyWord().getKeyWordsMerge(title, body, 10);             
                responseMsg = SplitTool.wordListToSplitStringSimple(words, false);
            }
            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();
    }
}

  

http 服务,布布扣,bubuko.com

http 服务

原文:http://www.cnblogs.com/nocml/p/3714140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!