3
我正在尝试编写一个简单的web服务器,该服务器在本地文件系统中使用java中的vertx服务器来处理html文件。出于某种原因,尽管我在资源文件夹中有我的web/index.html,但下面的代码没有找到该文件。我使用的是IntelliJ,它将此文件夹复制到它为项目生成的类文件夹中。如果我给绝对路径按预期工作。我在做什么错误,或者我怎么知道'web'文件夹是否是类路径的一部分? BTW,我已经通过使用的IntelliJ以及从终端运行这个测试,“MVN高管:JAVA -Dexec.mainClass =” test.vertx.VertxDriver”,但得到了同样的结果 - 未找到资源Vertx - 简单的web服务器找不到html文件
package test.vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.*;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import java.io.IOException;
public class VertxDriver {
private static final Logger logger = LoggerFactory.getLogger(VertxDriver.class);
public static void main(String[] args) {
VertxDriver driver = new VertxDriver();
Vertx vertx = VertxFactory.newVertx();
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(driver.new FileRequestHandler(vertx));
httpServer.listen(9999,"localhost");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
class FileRequestHandler implements Handler<HttpServerRequest> {
private Vertx vertx;
FileRequestHandler(Vertx vertx) {this.vertx = vertx;}
@Override
public void handle(HttpServerRequest httpServerRequest) {
String file = "";
if(httpServerRequest.path().equals("/")) {
file = "index.html";
}
logger.info("File being served is: "+file);
httpServerRequest.response().sendFile("web/"+file);
}
}
}
而我无法在stackoverflow上找到'vertx'标记,我没有足够的声望来创建一个。 – KumarM