FileUploadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LOGGER.info(String.format("Request Content-Type: %s, Content-Length:%d", req.getContentType(), req.getContentLength())); if (!MediaType.OCTET_STREAM.toString().equals(req.getContentType())) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Content type " + req.getContentType() + " is not supported"); return; } LOGGER.info("Creating temporary file"); File tempFile = File.createTempFile("selenium_node", ".zip"); try (ServletInputStream inputStream = req.getInputStream(); OutputStream outputStream = new FileOutputStream(tempFile)) { LOGGER.info("Copying request input stream to file"); IOUtils.copy(inputStream, outputStream); } LOGGER.info("Unzipping zip archive"); File imagesBaseDir = unZip(tempFile); if (!tempFile.delete()) { throw new IOException("Unable to delete file: " + tempFile); } LOGGER.info("Writing directory to response"); PrintWriter writer = resp.getWriter(); writer.write(imagesBaseDir.getAbsolutePath()); }    }
@Test public void testDoPost() throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http: httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString()); FileInputStream fileInputStream = new FileInputStream(zipArchive); InputStreamEntity entity = new InputStreamEntity(fileInputStream); httpPost.setEntity(entity); CloseableHttpResponse execute = httpClient.execute(httpPost); StatusLine statusLine = execute.getStatusLine(); assertThat(statusLine.getStatusCode(), equalTo(200)); try ( InputStream content = execute.getEntity().getContent()) { String directory = IOUtils.toString(content, StandardCharsets.UTF_8); unzippedArchive = new File(directory); unzippedFile = new File(directory + "/" + ZIP_FILE_NAME); } assertThat(unzippedFile.exists(), is(true)); try (FileInputStream unzippedFileStream = new FileInputStream(unzippedFile)) { String contents = IOUtils.toString(unzippedFileStream, StandardCharsets.UTF_8); assertThat(contents, is("test data")); } }