码迷,mamicode.com
首页 > 其他好文 > 详细

http 服务

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

标签:blog   class   code   java   tar   ext   

今天把一个功能模块做成了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 服务

标签:blog   class   code   java   tar   ext   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!