diff --git a/tomcat/src/main/java/org/apache/coyote/http11/handler/StaticFileHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/handler/StaticFileHandler.java index 7a2505f7ae..23923af08b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/handler/StaticFileHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/handler/StaticFileHandler.java @@ -9,6 +9,7 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.net.URL; import java.nio.charset.Charset; import static org.apache.coyote.http11.common.HttpStatus.OK; @@ -47,13 +48,14 @@ public static HttpResponse handle(final String requestURI, RequestHeader request } private static String findResponseBody(final String requestURI) throws IOException { - String requestedFile = ClassLoader.getSystemClassLoader().getResource("static" + requestURI).getFile(); - StringBuilder sb = new StringBuilder(); - BufferedReader br = new BufferedReader(new FileReader(requestedFile, Charset.forName("UTF-8"))); - String str; - while ((str = br.readLine()) != null) { - sb.append(str + "\n"); + URL requestedFile = ClassLoader.getSystemClassLoader().getResource("static" + requestURI); + try (BufferedReader br = new BufferedReader(new FileReader(requestedFile.getFile(), Charset.forName("UTF-8")))) { + StringBuilder sb = new StringBuilder(); + String str; + while ((str = br.readLine()) != null) { + sb.append(str + "\n"); + } + return sb.toString(); } - return sb.toString(); } }