1

I am building a quick HTTP server for my application and it is required to receive a large file (7z archive - 250MB to 1GB).

The server is based on com.sun.net.httpserver.HttpServer and com.sun.net.httpserver.HttpHandler.

So far I made the following which obviously does not work - it receive 44 bytes and finishes.

@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();
    }
}

The request look like this: enter image description here

How do I receive a file?

CorellianAle
  • 645
  • 8
  • 16
  • The problem doesn't seem to be about receiving the file. It seems to be about sending it. It' seems you're trying to send form-data, with a blank key and the file *name* as its value, whereas the server expects the request body to be the content of the file. You can probably use the "raw" tab of your client to make it send the file content as the request body. – JB Nizet Jan 03 '19 at 10:01
  • @JBNizet Yea, indeed. "raw" tab does not support file import, but "form" tab does. Now I need to extract form data from `InputStream` returned by `httpExchange.getRequestBody()`. – CorellianAle Jan 03 '19 at 10:08
  • No you don't. You need to use a client that is able to send a request body. See https://stackoverflow.com/questions/15912924/how-to-send-file-contents-as-body-entity-using-curl for example. – JB Nizet Jan 03 '19 at 10:09
  • @JBNizet Well, I switched to another client, passed the file as raw body and it worked. I am still confused about this `multipart` thing. Do I need to support it? Is it acceptable to just pass raw data? – CorellianAle Jan 03 '19 at 10:23
  • It's up to you. If you want a basic HTML form to be able to be used to send files to your server, then AFAIK yes, you'll need to support multipart. What are you writing this for? And why use this low-level Http server rather than a more production-ready server with a full-featured framework? – JB Nizet Jan 03 '19 at 10:35
  • @JBNizet I use low-level server because it was easier to understand in 30 minutes than a full-featured framework. Currently - it is a small application that only needs to receive a file (archive of files) from one client (admin) and make this file available to other clients (users). Clients are CRUD application that work with PostgreSQL. This program has been created because, the way I see it, it is a better approach that to store a 500+ MB in a database. – CorellianAle Jan 03 '19 at 11:06

0 Answers0