@Override public void handle(HttpServerRequest req) { Path p = Paths.get(assetRoot, req.path().substring(1)); if (fileSystem.existsSync(p.toString())) { super.handle(req); } else { req.response().end("File Not Found!"); } }
/** text/javascript text/css text/html; charset=GBK */ @Override public void handle(final HttpServerRequest req) { final HttpServerResponse res = req.response(); String uri = req.path().substring(1); if (uri.startsWith("web")) { uri = uri.substring(4); } if (uri.isEmpty()) { uri = indexPage; } String ext = Utils.getFileExtWithDot(uri); if (".js".equals(ext) || ".css".equals(ext) || ".html".equals(ext)) { int i = 0; if (".css".equals(ext)) { i = 1; } else { i = 2; } res.headers().set("content-type", ctary[i]); String finalFile = webRootPath.resolve(uri).toString(); // if(fileSystem.existsSync(finalFile)){ // fileSystem.readFile(finalFile, new AsyncResultHandler<Buffer>() { // public void handle(AsyncResult<Buffer> ar) { // if (ar.succeeded()) { // res.headers().set("content-length", ar.result().length() + ""); // res.end(ar.result()); // } else { // logger.error("Failed to open file", ar.cause()); // res.end("File Read Error!"); // } // } // }); // } else { // res.end("File Not Found!"); // } fileSystem.open( finalFile, new AsyncResultHandler<AsyncFile>() { public void handle(AsyncResult<AsyncFile> ar) { if (ar.succeeded()) { AsyncFile asyncFile = ar.result(); res.setChunked(true); Pump.createPump(asyncFile, res).start(); asyncFile.endHandler( new VoidHandler() { public void handle() { res.end(); } }); } else { logger.error("Failed to open file", ar.cause()); res.end("File Not Found!"); } } }); } else { res.sendFile(webRootPath.resolve(uri).toString()); } }