Reputation: 17
I use NanoHTTP to set up a web server with HTML, JS and some Java. Now I want to get the JavaScript resource out of the HTML by using
private String readFileAsTextUsingInputStream(String filename) {
if(Objects.equals(filename, "/login")) {
filename = "/Users/myName/Desktop/Arbeit/Lehrerkalender/src/main/resources/html/login.html";
}
return Connection.class.getResourceAsStream(filename).readAllBytes().toString();
}
inside my Connection.java. This function is called in the "serve" method. The serve method (I think) is being called, when you open the website or change the session uri. This method looks like this:
@Override
public Response serve(IHTTPSession session) {
String msg = null;
if (Objects.equals(session.getUri(), "/login")) {
try {
msg = readFileAsTextUsingInputStream(session.getUri());
} catch (IOException e) {
e.printStackTrace();
}
return newFixedLengthResponse(msg);
} else if(session.getUri().isBlank() || Objects.equals(session.getUri(), "/")) {
msg = "<html><body><h1>Die Website finden Sie<a href=http://"+this.getHostname()+":"+
this.getListeningPort()+"/login>hier</a></h1></body></html>".trim();
}
return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, getMimeType(session.getUri()), msg);
}
So, when I start the program and go to "http://localhost:9080/login" the program tries to identify the HTML and JS but already fails at the HTML. I've also tried it with giving the absolute path and path from the root and from the class where this is being called from. I also tried using ClassLoader and everything but it always tells me that the return value of "java.lang.Class.getResourceAsStream(String)" is null.
The project is build like this:
Thanks already for your help!
Upvotes: 0
Views: 1450