-5

In How to export data in CSV format using Java?, I found a solution to write the date to CSV files and the accepted answer works fine.

I need change file name to anything with .csv extention. Now I get WebServlet name as a file name without any extention.

Do you have any idea how to set a file name?

resp.setContentType("application/csv");
PrintWriter w = resp.getWriter();
w.println(generateCsvFile(policies));
w.flush();
w.close();
Donald Duck
  • 7,638
  • 19
  • 69
  • 90
plucins
  • 143
  • 2
  • 11

1 Answers1

2

You need to set a file name to the header:

response.setHeader("Content-Disposition", "attachment; filename='" + filename + "'");

The browser will honor the header and use the file name for downloading file

Mạnh Quyết Nguyễn
  • 16,814
  • 1
  • 21
  • 47