title: How to generate word document using existing template using POI, Spring MVC, jquery?
body: <p>I tried some code but it's downloading blank document. If I try to write HWPFDocument object on the response.getOutputStream() then word document is getting corrupted. Need help.</p>
  
  <p>Jquery function</p>
  
  <pre><code>$.ajax({ type : "GET",
          url : urlIpd,
          success : function(data) {
          $("#dl-frames").attr("src", urlIpd);
      },
  
  });
  </code></pre>
  
  <p>Controller method.</p>
  
  <pre><code>@RequestMapping(value = "/{partLocnId}/{partnerId}/ipdDwnld", method = RequestMethod.GET)
      public void ipdDwnld(@PathVariable("partLocnId") String partLocnId, @PathVariable("partnerId") String partnerId, HttpServletResponse response) {
          HWPFDocument doc = null;
          try {
  
              response.setContentType("application/msword");
              response.addHeader("Content-Disposition", "attachment; filename=\"" + partnerId +".doc" + "\";");
  
              POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream((new File("C:/templates/IPDV14Q117.ImplementationName.doc"))));
              doc = new HWPFDocument(fs);
              doc.getRange();
  
              Range range = doc.getRange();
              range.replaceText("Integration Process Design", "TEST");
  
              //ServletOutputStream fos = (response.getOutputStream()); //using this document will get corrupt.
              FileOutputStream fos = new FileOutputStream(new File(partnerId+".doc")); // generates blank document
              doc.write(fos);
  
              fos.close();                
              doc.close();
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>Thanks in advance</p>