package handlers; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.nio.file.Files; public class FileHandler implements HttpHandler { public void handle(HttpExchange exchange) throws IOException { boolean success = false; try { if (!exchange.getRequestMethod().equals("GET")) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_METHOD, 0); exchange.getResponseBody().close(); } else { String urlPath = exchange.getRequestURI().toString(); if (urlPath.equals(null) || urlPath.equals("/")) { urlPath = "/index.html"; } String filePath = "web" + urlPath; File file = new File(filePath); if (!file.exists()) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, 0); OutputStream responseBody = exchange.getResponseBody(); File notFound = new File("web/HTML/404.html"); Files.copy(notFound.toPath(), responseBody); exchange.getResponseBody().close(); } exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); OutputStream responseBody = exchange.getResponseBody(); Files.copy(file.toPath(), responseBody); exchange.getResponseBody().close(); success = true; } if (!success) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, 0); exchange.getResponseBody().close(); } } catch (IOException e) { exchange.sendResponseHeaders(HttpURLConnection.HTTP_SERVER_ERROR, 0); exchange.getResponseBody().close(); e.printStackTrace(); } } }