-4

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
  • 8,409
  • 22
  • 75
  • 99
plucins
  • 153
  • 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
  • 17,677
  • 1
  • 23
  • 51