title: MSWord File download issue using HttpServlet
body: <p>i have implemented a <code>servlet</code> to download doc files available under my application classpath.</p>
  
  <p>what happening is; file is downloading but <code>ms-word</code> is unable to open it property.
  see the screenshot of <code>ms-word</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/b8wsi.jpg" alt="msword screenshot"></p>
  
  <p><code>Servlet</code> implementation is as follows:</p>
  
  <pre><code>public class DownloadFileServlet extends HttpServlet {
  
      protected void doGet(
          HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
  
          String fileName = "test.doc";
          ClassPathResource resource = new ClassPathResource(File.separator + fileName);
          ServletOutputStream sos = null;
          FileInputStream fis = null;
  
          try {
              response.setContentType("application/msword");
              response.setHeader("Content-disposition", "attachment; fileName=\"" + fileName + "\"" );
  
              fis = new FileInputStream(new File(resource.getURI().getPath()));
              byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(fis);
  
              sos = response.getOutputStream();
              sos.write(bytes);
              sos.flush();
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
          if( fis != null) {
              fis.close();
          }
          if( sos != null) {
              sos.close();
          }
          }
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request, response);
      }
  }
  </code></pre>
  
  <p>i have tried almost all suggested Content Types for <code>ms-word</code> files. but still it is not working.</p>
  
  <pre><code>application/msword
  application/ms-word
  application/vnd.ms-word
  </code></pre>
  
  <p>Kindly suggest I'm making a mistake or is there any other way to achieve.</p>
  
  <p>Note: i have tried almost all approaches available on SO. </p>