Ява HttpServer получает большой файл в HttpHandler - PullRequest
0 голосов
/ 03 января 2019

Я создаю быстрый HTTP-сервер для своего приложения, и требуется получить большой файл (архив 7z - от 250 МБ до 1 ГБ).

Сервер основан на com.sun.net.httpserver.HttpServer и com.sun.net.httpserver.HttpHandler.

До сих пор я сделал следующее, что, очевидно, не работает - он получает 44 байта и завершается.

@Override
public void handle(HttpExchange httpExchange) throws IOException
{
    String requestMethod = httpExchange.getRequestMethod();

    if (requestMethod.equalsIgnoreCase("POST"))
    {
        Headers responseHeaders = httpExchange.getResponseHeaders();
        responseHeaders.set("Content-Type", "text/plain");
        httpExchange.sendResponseHeaders(200, 0);

        InputStream inputStream = httpExchange.getRequestBody();
        byte[] buffer = new byte[4096];
        int lengthRead;
        int lengthTotal = 0;
        FileOutputStream fileOutputStream = new FileOutputStream(FILENAME);

        while ((lengthRead = inputStream.read(buffer, 0, 4096)) > 0)
        {
            fileOutputStream.write(buffer, 0, lengthRead);
            lengthTotal += lengthRead;
        }

        fileOutputStream.close();

        System.out.println("File uploaded - " + lengthTotal + " bytes total.");

        httpExchange.getResponseBody().close();
    }
}

Запрос выглядит так: enter image description here

Как получить файл?

...