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();
}
}
How do I receive a file?