Search code examples
javahttpserver

Can't host MP4 com.sun HttpServer


I made a basic HTTP server using com.sun.net.httpserver API. When I use createContext, it seems to work fine for everything except video files. Here is the HttpHandler I'm using in the createContext statement:

private static HttpHandler load(final String fileName) {
    return new HttpHandler() {
        @Override
        public void handle(HttpExchange t) throws IOException {
            File file = new File(fileName);
            FileInputStream fin = new FileInputStream(file);

            OutputStream os = t.getResponseBody();

            final byte[] buffer = new byte[0x10000];
            int count = 0;
            while ((count = fin.read(buffer)) >= 0) {
                t.sendResponseHeaders(200, count);
                os.write(buffer, 0, count);
            }

            os.flush();
            os.close();
            fin.close();
        }
    };
}

I'm not getting any errors. When I open the web page to view the video, it'll just show an empty video frame. My HTML itself is fine, as I tried it without hosting the server and it worked. How can I fix this?


Solution

  • So the issue ended up being that I was adding an mp4, we'll call test.mp4 to an endpoint, we'll call it /example, but when I referenced the mp4 in my HTML file I did /example/test.mp4 instead of just /example.