import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yunva.vertx.test.vertproject.util.JsonUtil;

import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;

public class TestServer {

private static final Logger logger = LoggerFactory.getLogger(Server.class);

public static void main(String[] args){
HttpServerOptions options = new HttpServerOptions().setMaxWebsocketFrameSize(1000000);//Server配置

Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer(options);//创建Server

server.requestHandler(request -> {
request.handler(buffer -> {//从body中读取数据
System.out.println("I have received a chunk of the body of length " + buffer.length());
Map<String, Object> map = JsonUtil.jsonToMap(buffer.toString());
System.out.println("name:" +map.get("name") + " school" + map.get("school"));
});

System.out.println("you have recieved a request from " + request.uri());
System.out.println("you have recieved a request from " + request.absoluteURI());//绝对路径
Map<String, Object> map = new HashMap<>();
map.put("name", "hello");
map.put("sdfa", "dsfakd");
//设置返回头
HttpServerResponse response = request.response();
MultiMap headers = response.headers();
headers.set("content-type", "text/html");
headers.set("other-header", "wibble");
request.response().setChunked(true).write(map.toString()).end("you get a reply from the server");
});
server.listen(8080, "127.0.0.1");
}
}
/*
* vertx创建服务接收请求并返回:
* 创建HttpServer-->server.requestHandler---》request.handler---》request.response()
*
* 处理表单请求
* 表单请求分为两种方式:
* 1 普通请求 application/x-www-form-urlencoded
* 2 多文件请求 multipart/form-data
* 对于普通请求,起参数均可以从URL中获取,可像处理一般请求一样处理
* 对于multipart/form-data请求
* server.requestHandler(request -> {
request.setExpectMultipart(true);
request.endHandler(v -> {
// The body has now been fully read, so retrieve the form attributes
MultiMap formAttributes = request.formAttributes();
});
});
处理文件上传
server.requestHandler(request -> {
request.setExpectMultipart(true);
request.uploadHandler(upload -> {
System.out.println("Got a file upload " + upload.name());
});
});

File uploads can be large we don’t provide the entire upload in a single buffer as that might result in
memory exhaustion, instead, the upload data is received in chunks:

request.uploadHandler(upload -> {
upload.handler(chunk -> {
System.out.println("Received a chunk of the upload of length " + chunk.length());
});
});
*
*
*
* */

最新文章

  1. SQL Server 执行计划利用统计信息对数据行的预估原理以及SQL Server 2014中预估策略的改变
  2. 记录视频“ Why I build Docker&quot;
  3. 黄聪:wordpress向一个页面POST数据,出现404页面访问不了
  4. C#修改文件夹权限
  5. 【MVC 4】6.SportsSore:导航
  6. 导入maven项目后无法找到sun tools toos-15.0.jar
  7. webx学习笔记
  8. Node.js之错误处理与断言处理
  9. 从零开始学习前端开发 — 7、CSS宽高自适应
  10. easyUI 创建有复选框的table.datagrid
  11. C# 一款属于自己的音乐播放器
  12. centos下Django+uwsgi+nginx
  13. (网页)jQueryAJAXtimeout超时问题详解(转)
  14. 走进JDK(五)------AbstractList
  15. nginx服务器常见错误代码500、501、502、503、504、505
  16. div展开与收起(鼠标点击)
  17. npm常规命令行集合
  18. 认识与入门 MarkDown 标记语言
  19. Linux系统下便捷使用中国知网的方式
  20. iis 应用程序连接池 在计算机“.”上没有找到WAS服务

热门文章

  1. 后台js弹提示
  2. NSQ的消息订阅发布测试
  3. React Native 实现页面动态切换
  4. 那些可能被你忽略的MySQL优化技巧
  5. APS.net controller
  6. flash开发几个问题
  7. Basic Calculator
  8. iOS之分别使用代码和storyboard、xib为控件设置圆角(以按钮为例)
  9. js对象的继承以及公有私有属性的定义和读写
  10. 无cookie下载Oracle otn上的sun jdk等软件