How to load csv file using java servlet?

I have a sample Java servlet file.but, this is an export to a local file. But do I need to download the csv file when I click the download button?

here is the servlet class, what code do i need to add here to load the csv file?

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CsvFile extends HttpServlet { 
public void doGet (HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException  {
try
{
      PrintWriter out = response.getWriter();
      String filename = "c:\\csv\\myfile.csv";
      FileWriter fw = new FileWriter(filename);

      fw.append("Employee Code");
      fw.append(',');
      fw.append("Employee Name");
      fw.append(',');
      fw.append("Employee Address");
      fw.append(',');
      fw.append("Employee Phone");
      fw.append(',');
      fw.append("Employee ZipCode");
      fw.append('\n');

      fw.append("E1");
      fw.append(',');
      fw.append("Vineet");
      fw.append(',');
      fw.append("Delhi");
      fw.append(',');
      fw.append("224277488");
      fw.append(',');
      fw.append("110085");
      fw.append('\n');

      fw.append("E2");
      fw.append(',');
      fw.append("Amar");
      fw.append(',');
      fw.append("Delhi");
      fw.append(',');
      fw.append("257765758");
      fw.append(',');
      fw.append("110001");
      fw.append('\n');

      fw.append("E3");
      fw.append(',');
      fw.append("Amit");
      fw.append(',');
      fw.append("Delhi");
      fw.append(',');
      fw.append("257685858");
      fw.append(',');
      fw.append("110005");
      fw.append('\n');

      fw.append("E4");
      fw.append(',');
      fw.append("Suman");
      fw.append(',');
      fw.append("Delhi");
      fw.append(',');
      fw.append("266447678");
      fw.append(',');
      fw.append("110081");
      fw.append('\n');


      fw.flush();
      fw.close();
      out.println("<b>Csv file Successfully created.</b>");

} 
catch (Exception ex) {
ex.printStackTrace ();
}
}
}
+3
source share
2 answers

You are writing a file instead of an HTTP response.

  • You need to record CSV in HttpServletResponse#getWriter().
  • Content-Disposition attachment, " " -, filename. (?) : MSIE filename " ", URL-.
  • Content-Type text/csv, webbrowser, , , Open Save. Windows- MS Excel .

, CsvServlet, doGet().

String filename = request.getPathInfo().substring(1); // get rid of leading `/`
response.setHeader("Content-Type", "text/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
PrintWriter writer = response.getWriter();
writer.append("CSV content");
// ...

. flush() close(), , , , , - - ( , IllegalStateException / IOException , , .

CsvServlet web.xml url-pattern of /csv/* http://example.com/context/csv/filename.csv.


, , , / CSV, String[][] List<List<String>> OutputStream Writer, CSV. , , CSV .

. :

+4

application/vnd.ms-excel response.setHeader("Content-Disposition", "attachment; filename=\"myfile.csv\"");

+3

Source: https://habr.com/ru/post/1752879/


All Articles