How to download CSV files using java servlets?

I have a sample java servlet file But it is exported 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 download CSV files?

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("257765758");
      fw.append(',');
      fw.append("110001");
      fw.append('\n');

      fw.append("E3");
      fw.append(',');
      fw.append("Amit");
      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("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 ();
}
}
}

Solution

You are writing to a file instead of an HTTP response

>You need to write CSV to httpservletresponse #getwriter(). > You need to set the content disposition header as an attachment to force the Save As dialog box in the web browser, and finally the file name attribute There is one (main?) Warning: MSIE browser will not use the specified file name as the actual file name in the Save As dialog box, but use the last part of pathinfo of the URL. > You need to set the content type header to text / CSV to indicate what type of file the WebBrowser is, so that you can find the correct associated application when the end user selects open instead of save Usually, on Windows machines, MS Excel is associated with this content type by default

To achieve these requirements, you need to create a csvservlet, which basically performs the following operations in the doget () method

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");
// ...

That's it. Flush () and close () by the way, are not absolutely necessary, but if you want to avoid other contents in the request chain, you are adding something to the response body (this should strictly not happen, but it will only issue) illegalstateexceptions and / or ioexceptions into the server log instead of the wrong response

Then, use the URL pattern of / CSV / * to map the web Csvservlet in XML and in http://example.com/context/csv/filename.csv Call it before.

In other words, you may be more like a real CSV formatter / writer. You can write string []] or list < list < string > > to OutputStream or writer well. Therefore, you respect the CSV format rules The field value itself may contain quotes or commas, and then the CSV format will break

You can also see:

> JSP page without HTML code for exporting data to Excel Sheet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>