title: Parsing Binary Data from HttpServletRequest
body: <p>What is the general approach to retrieve binary data that is posted to a Java Servlet?  A byte[] is being posted to this servlet and I think I have to somehow parse the HttpServletRequest.getInputStream() and pull out the byte[] contents.  Any ideas on how to change the below code to accomplish this?</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          StringBuilder stringBuilder = new StringBuilder();
          BufferedReader bufferedReader = null;
          try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
             bufferedReader = new BufferedReader(new InputStreamReader(
          inputStream));
             char[] charBuffer = new char[128];
             int bytesRead = -1;
             while ((bytesRead = bufferedReader.read(charBuffer)) &gt; 0) {
              stringBuilder.append(charBuffer, 0, bytesRead);
             }
            } else {
             stringBuilder.append("");
            }
          } catch (IOException ex) {
            throw ex;
          } finally {
            if (bufferedReader != null) {
             try {
              bufferedReader.close();
             } catch (IOException ex) {
              throw ex;
             }
            }
          }
          String body = stringBuilder.toString();
          System.out.println(body);
  }
  </code></pre>