Java httpserver cannot send large files

44 Views Asked by At

There are no error on the server side, but clients like Chrome and the application that needs the file can't download it. The server works with smaller files and it basically runs like this:

package org.org.APIServer.api;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class APIServer {
    public static void startAPIServer() {
        try {
            HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
            server.createContext("/api", new APIHandler());
            server.setExecutor(null);
            server.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static class APIHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            byte[] responseBytes = Files.readAllBytes(Path.of("pack.zip"));
            t.getResponseHeaders().set("Content-Type", "application/octet-stream");
            t.sendResponseHeaders(200, responseBytes.length);
            OutputStream os = t.getResponseBody();
            os.write(responseBytes);
            os.close();
        }
    }
}

My test file is 1GB btw

0

There are 0 best solutions below