使用Java內建的Http Server構建Web應用
阿新 • • 發佈:2018-12-27
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] arg) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
server.createContext("/test", new TestHandler());
server.start();
}
static class TestHandler implements HttpHandler{
@Override
public void handle(HttpExchange exchange) {
new Thread(new Runnable() {
@Override
public void run() {
try{
String response = "hello world";
//獲得查詢字串(get)
String queryString = exchange.getRequestURI().getQuery();
Map<String,String> queryStringInfo = formData2Dic(queryString);
//獲得表單提交資料(post)
String postString = IOUtils.toString(exchange.getRequestBody());
Map<String,String> postInfo = formData2Dic(postString);
exchange.sendResponseHeaders(200,0);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}catch (IOException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
public static Map<String,String> formData2Dic(String formData ) {
Map<String,String> result = new HashMap<>();
if(formData== null || formData.trim().length() == 0) {
return result;
}
final String[] items = formData.split("&");
Arrays.stream(items).forEach(item ->{
final String[] keyAndVal = item.split("=");
if( keyAndVal.length == 2) {
try{
final String key = URLDecoder.decode( keyAndVal[0],"utf8");
final String val = URLDecoder.decode( keyAndVal[1],"utf8");
result.put(key,val);
}catch (UnsupportedEncodingException e) {}
}
});
return result;
}
}