[英]How to get download csv file using java servlet?
I have sample java servlet file.but it is export to local file.But i need to download the csv file when the hit download button ? 我有示例java servlet文件。但它导出到本地文件。但是我需要在点击下载按钮时下载csv文件?
here is servlet class , what code i need to add here for download the csv file ? 这里是servlet类,我需要在这里添加什么代码来下载csv文件?
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 ();
}
}
}
You're writing to a file instead of to the HTTP response. 您正在写入文件而不是HTTP响应。
HttpServletResponse#getWriter()
. HttpServletResponse#getWriter()
。 Content-Disposition
header to attachment
to force a Save As dialogue in the webbrowser, eventually along with a filename
attribute. Content-Disposition
标头设置为attachment
以强制Web浏览器中的“ 另存为”对话框,最终还有filename
属性。 There's one (major?) caveat: the MSIE browser won't use the specified filename
as actual file name in the Save As dialogue, it will instead use the last part of the pathinfo of the URL. filename
作为实际文件名,而是使用URL的pathinfo的最后一部分。 Content-Type
header to text/csv
to instruct the webbrowser what kind of file it is so that it can find the correct associated application when the enduser chooses to Open instead of Save . Content-Type
标头设置为text/csv
以指示webbrowser它是什么类型的文件,以便在最终用户选择Open而不是Save时找到正确的关联应用程序。 Usually, on Windows Machines, MS Excel is by default associated with that content type. To achieve those requirements, you need to create a CsvServlet
which does basically the following in the doGet()
method. 要实现这些要求,您需要创建一个
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");
// ...
That's all. 就这样。 The
flush()
and close()
are by the way not strictly necessary, but useful if you want to avoid that something else further in the request chain is appending something to the response body (which should strictly not happen, but it would only emit IllegalStateException
s and/or IOException
s into the server logs instead of malforming the response. flush()
和close()
顺便说一下并不是绝对必要的,但是如果你想避免请求链中的其他内容正在向响应体添加一些东西(这应该严格不会发生,但它只会发出) IllegalStateException
和/或IOException
进入服务器日志而不是错误的响应。
Then, map the CsvServlet
in web.xml
with an url-pattern
of /csv/*
and invoke it by http://example.com/context/csv/filename.csv . 然后,使用
/csv/*
的url-pattern
映射web.xml
的CsvServlet
,并通过http://example.com/context/csv/filename.csv调用它。
That said, you'd probably rather like a real CSV formatter/writer which nicely writes a String[][]
or a List<List<String>>
to an OutputStream
or Writer
, hereby respecting the CSV formatting rules. 也就是说,您可能更喜欢真正的CSV格式化程序/编写器,它可以很好地将
String[][]
或List<List<String>>
写入OutputStream
或Writer
,从而遵守CSV格式规则。 It might happen that a field value itself contains a quote or a comma, the CSV format would then break. 字段值本身可能包含引号或逗号,然后CSV格式会中断。
将内容类型设置为application/vnd.ms-excel
和content-disposition response.setHeader("Content-Disposition", "attachment; filename=\\"myfile.csv\\"");
响应头response.setHeader("Content-Disposition", "attachment; filename=\\"myfile.csv\\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.