title: How to download CSV file from GUI from java spring controller on the go?
body: <p>I am new in Software Development, just out of college. Doing my first big project. </p>
  
  <p>I am trying to download the CSV file when the user chooses the start date and end date of the project, so the file is supposed to return the project.csv with project name, date from .. to .. </p>
  
  <p>The most important thing is that I am trying to download the file from GUI after clicking on "export" link. All I know is I have to make a spring controller. I have to be missing some part as it's not working.
  My java class is writing the csv file to my C disk but it doesn't do the download part. Also CSV file should be written from the database to users computer, not to my disk.</p>
  
  <p>Hope you understand me. Let me know if that's clear.</p>
  
  <p>my code:</p>
  
  <p><strong>ExportController.java</strong></p>
  
  <pre><code>    @RestController
      @RequestMapping("/config")
      public class ExportController {
           private String filePath = "C:\\test\\project.csv";
           private static final int BUFFER_SIZE = 4096;
  
          @Autowired
          private ExportService exportService;
          ServletContext context;
  
          @RequestMapping(value = "export/all", method = RequestMethod.POST)
          public String list(@RequestParam("startDate")String date, @RequestParam("endDate")String date2, HttpServletResponse response, HttpServletRequest request) throws ServletException, ParseException, IOException {
              DateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
              Date date_obj = format.parse(date);
              Date date2_obj = format.parse(date2);
  
              // get absolute path of the application
              ServletContext context = request.getServletContext();
              String appPath = context.getRealPath("");
              System.out.println("appPath = " + appPath);
  
              // construct the complete absolute path of the file
              String fullPath = filePath;      
              File downloadFile = new File(fullPath);
              FileInputStream inputStream = new FileInputStream(downloadFile);
  
              // get MIME type of the file
              String mimeType = context.getMimeType(fullPath);
              if (mimeType == null) {
                  // set to binary type if MIME mapping not found
                  mimeType = "application/octet-stream";
              }
  
              System.out.println("MIME type: " + mimeType);
  
              CsvWriterProject.savetofile();
  
              String csv = "Employee FN/LN: Eatrick Hughes Contract type: External, Activity: WAR, Effort date: 2016-02-17, Name: udfuhfd, Entity: BA, Start date: 2016-02-17, End_date: 2016-02-18";
  
              response.setContentType("application/csv");
              response.setHeader("Content-Disposition", "attachment; filename=project.csv"); 
              response.setHeader("Pragma", "public");
              response.setHeader("Expires", "0");
              response.setHeader("Content-Length", String.valueOf(csv.length()));
              response.setHeader("Content-type","application/csv"); 
          //  response.setContentType(mimeType);
          //  response.setContentLength((int) downloadFile.length());
              // get output stream of the response
              OutputStream outStream = response.getOutputStream();
              PrintWriter pw = new PrintWriter(outStream);
              pw.print(pw);
  
  
              byte[] buffer = new byte[BUFFER_SIZE];
              int bytesRead = -1;
  
              // write bytes read from the input stream into the output stream
              while ((bytesRead = inputStream.read(buffer)) != -1) {
                  outStream.write(buffer, 0, bytesRead);
              }
              inputStream.close();
              outStream.flush();
              outStream.close();
              return csv;
  
  
          }
      }
  </code></pre>
  
  <p>Here is <strong>angularJS</strong></p>
  
  <pre><code>$scope.export_all = function(item){
           $http.post('config/export/all?startDate='+item.startDate+"&amp;endDate="+item.endDate)
           .success(function(response) {
               $scope.export = response;
  
                  });
      };
  </code></pre>
  
  <p>Let me know if you need more information.</p>