zoukankan      html  css  js  c++  java
  • vertx简单服务创建

    直接上代码

     

    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的个数统计
    组合以及可以重复的排列
    Swing之图书管理系统
    经常熬夜的人一定要看
    利用SharePoint Designer开发可循环工作流
    SharePoint Software DownLoad Address 微软软件 下载地址
    日期验证 正则表达式
    时间验证 正则表达式
    Reporting Services SharePoint AddIn Setup Problems
    char、varchar、text和nchar、nvarchar、ntext的区别
  • 原文地址:https://www.cnblogs.com/canmeng-cn/p/5938980.html
Copyright © 2011-2022 走看看