title: How to download file in browser using spring mvc?
body: <p>I have file on the server &amp; that I want to download on my machine using browser. But I am not getting an option from browser to download the file.</p>
  
  <p>My code is </p>
  
  <p>JSP</p>
  
  <pre><code>&lt;div id="jqgrid"&gt;
      &lt;table id="grid"&gt;&lt;/table&gt;
      &lt;div id="pager"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>jq("#grid").jqGrid({
      ....
  
      onCellSelect: function(rowid, index, contents, event) {
      ...
         var fileName = jQuery("#grid").jqGrid('getCell',rowid,'fileName');
         $scope.downloadFile(fileName);
      }
  });
  
  
  $scope.downloadFile = function(fileName) {
      $http({
          url: "logreport/downLoadFile", 
          method: "GET",
          params: {"fileName": fileName}
       });
  };
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@RequestMapping(value = "/downLoadFile", method = RequestMethod.GET)
  public void downLoadFile(HttpServletRequest request, HttpServletResponse response) {
      try {
          String fileName = request.getParameter("fileName");
          File file = new File(filePath +"//"+fileName);
          InputStream in = new BufferedInputStream(new FileInputStream(file));
  
          response.setContentType("application/xlsx");
          response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xlsx"); 
  
  
          ServletOutputStream out = response.getOutputStream();
          IOUtils.copy(in, out);
          response.flushBuffer();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I am not getting any exception but not sure why browser dialog is not opening to download the file. Also where is it exactly downloading the file?</p>