title: Download from browser files stores on server through Rest web services
body: <p>I have a project based on Spring (Spring security, spring controller e MVC ) so in my HTML page I have to allow file download.
  When user click on this :</p>
  
  <pre><code>&lt;a href="#" target="'+row.idCar+'" id="logName" name="logDownload"&gt; &lt;i class="fa fa-file-text-o fa-2x" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;'
  </code></pre>
  
  <p>I handle event and use this code based on <a href="http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/" rel="nofollow noreferrer">jquery plugin</a></p>
  
  <pre><code>var input = $(this),
          idCar= $(this).attr('target');
          $.fileDownload("../download/" + idCar , {
              successCallback: function (url) {
              },
              failCallback: function (HttpServletResponse, url) {
                  notifyMessage("Error downloading file", 'error');
              }
          });
          return false; //this is critical to stop the click event which will trigger a normal file download! 
  </code></pre>
  
  <p>In my controller:</p>
  
  <pre><code>@Override
  @RequestMapping(value="/download/{idCar}", method = RequestMethod.GET)
  public void download(HttpServletResponse response, @PathVariable("idCar") int idCar) {
      try{
          File file = fleetAndCarService.getLogFile(idCar);
          try(InputStream is = new FileInputStream(file.getAbsolutePath())) {
              response.setHeader("Set-Cookie", "fileDownload=true; path=/");
              response.setHeader("Content-Disposition",
                          "attachment; filename=\"" + "AcquisitionLog.txt" + "\"");
              org.apache.commons.io.IOUtils.copy(is,
                          response.getOutputStream());
              response.flushBuffer();
          } catch (Exception e1) {
              ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e1);
              LOG.error("Threw exception in FleetAndCarControllerImpl::download :" + errorResponse.getStacktrace());
          }
      }catch(Exception e){
          ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
          LOG.error("Threw exception in FleetAndCarControllerImpl::download :" + errorResponse.getStacktrace());
      }
  }
  </code></pre>
  
  <p>But it has problem with Spring Security like this post <a href="https://stackoverflow.com/questions/36603174/x-frame-deny-in-spring-security">x-frame</a> and on internet explorer doesn't work. 
  So, there is a way to download file stored on my server without use of plugin?
  Thanks</p>
  
  <p><strong>RESOLVED</strong>:  Hansraj helped me and this is the working code:</p>
  
  <pre><code>try{
              File file = fleetAndCarService.getLogFile(idCar);
              try(InputStream is = new FileInputStream(file.getAbsolutePath())){
                  response.setHeader("Expires", "0");
                  response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
                  response.setContentType("text/plain");
                  response.setHeader("Content-Description"," File Transfer");
                  response.setHeader("Content-Disposition","attachment; filename="+"AcquisitionLog.txt");
                  response.setHeader("Content-Transfer-Encoding"," binary");
                  long size = Utils.stream(is, response.getOutputStream());
                  response.addHeader("Content-Length", Long.toString(size));
                  response.flushBuffer();
              }catch (Exception e1){
                  ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e1);
                  LOG.error("Threw exception in FleetAndCarControllerImpl::download :" + errorResponse.getStacktrace());
              }
          }catch (Exception e) {
              ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
              LOG.error("Threw exception in FleetAndCarControllerImpl::download :" + errorResponse.getStacktrace());
          }
  </code></pre>
  
  <p>in the HTML I used href with the url of web service.
  Utils is the class suggested in this <a href="https://stackoverflow.com/a/10142601/4004025">post</a></p>