title: How to read contents of a multipart file inputstream in Java
body: <p>I have a Thymeleaf html form that accepts an uploaded file as input and then makes a post request to a Java controller for the multipart file. I then convert the file into an inputstream. While I am able to read the file's size and input type, I am not able to successfully print out the contents.</p>
  <p>For example, for a .doc file, if I try methods I have found to print out the file's contents, it merely prints a series of numbers. Which I'm assuming is an encoding. Does a method exist to print out the contents of an uploaded .doc file?</p>
  <p>The controller action I'm currently using to attempt to print out the file's contents is:</p>
  <pre><code>@PostMapping(&quot;/file-upload&quot;)
      public String uploadFile(@RequestParam(&quot;fileUpload&quot;) MultipartFile fileUpload, Model model) throws IOException {
          InputStream fis = fileUpload.getInputStream();
  
          for (int i = 0; i &lt; fis.available(); i++) {
              System.out.println(&quot;&quot; + fis.read());
          }
  
          return &quot;home&quot;;
  }
  </code></pre>
  <p>And the form I am using to submit the file is:</p>
  <pre><code>                        &lt;form th:action=&quot;@{/file-upload}&quot; enctype=&quot;multipart/form-data&quot; method=&quot;POST&quot;&gt;
                              &lt;div class=&quot;container&quot;&gt;
                                  &lt;div class=&quot;row&quot; style=&quot;margin: 1em;&quot;&gt;
                                      &lt;div class=&quot;col-sm-2&quot;&gt;
                                          &lt;label for=&quot;fileUpload&quot;&gt;Upload a New File:&lt;/label&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;col-sm-6&quot;&gt;
                                          &lt;input type=&quot;file&quot; class=&quot;form-control-file&quot; id=&quot;fileUpload&quot; name=&quot;fileUpload&quot;&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;col-sm-4&quot;&gt;
                                          &lt;button type=&quot;submit&quot; class=&quot;btn btn-dark&quot;&gt;Upload&lt;/button&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
  </code></pre>