public class Main {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new BaseHandler());
server.setExecutor(null);
server.start();
}
static class BaseHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
Headers h = t.getResponseHeaders();
String line;
StringBuilder resp = new StringBuilder();
try {
File newFile = new File("My absolute file path");
System.out.println("Rendering file: " + newFile.getName());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(newFile)));
while ((line = bufferedReader.readLine()) != null) {
resp.append(line);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
h.add("Content-Type", "application/json");
t.sendResponseHeaders(200, resp.length());
OutputStream os = t.getResponseBody();
os.write(resp.toString().getBytes());
os.close();
}
}
}
#Basic java webserver rendering html too literally
12 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @median widget! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
As stated above, the response for my html is the actual raw text of the html
I have a general understanding but it doesn't like me
Change content type to text/html
Did it work