使用Java在浏览器中下载CSV文件 [英] Downloading a CSV File in Browser by using Java

查看:1825
本文介绍了使用Java在浏览器中下载CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个web应用程序上添加一个按钮,当点击它时,下载一个CSV文件(这是一个很小的,只有大约4KB)。



我已经建立了按钮,并且附加了一个监听器。该文件也准备好了。
现在我唯一需要做的是创建一个实际的事件,当点击按钮时下载csv文件。



假设file是File类型)是我正在使用的csv文件的名称,它已经准备好了,这是我到目前为止的方法:

  public void downloadCSV(HttpServletRequest _request,HttpServletResponse _response,LogFileDetailCommand cmd)throws Exception {
ServletOutputStream outStream = _response.getOutputStream();
_response.setContentType(text / csv);
_response.setHeader(Content-Disposition,attachment; filename = data.csv);
}

我意识到我缺少将文件写入ServletOutputStream但这是我坚持的部分。我知道类ServletOutputStream扩展类OutputStream( http:// docs。 oracle.com/javaee/5/api/javax/servlet/ServletOutputStream.html ),因此它已经继承了它的写方法,所以在理想的世界中,我只想说outStream.write (file);,但是我不能这样看待,因为这些write方法只有byte,byte []和int作为参数,并且不可能传入一个文件。



我应该如何将文件传递到outStream?



(另请注意:我应该在_response.setContentLength();中添加此属性,如果是这样,我应该将什么参数放入?)



Ps我知道在我完成outStream之后,我需要刷新和/或关闭它。

解决方案

CSV文件,并将该行写入 HttpSerlvetResponse : $ Writer b
$ b

  public class DownloadCsvServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse响应)throws ServletException,IOException {

final文件文件=新文件(/ tmp / data.csv);

response.setContentType(text / csv);

response.setHeader(Content-Disposition,
attachment; filename =+ file.getName());

final BufferedReader br = new BufferedReader(new FileReader(file));
try {
String line;
while((line = br.readLine())!= null){
response.getWriter()。write(line +\\\
);
}
} finally {
br.close();
}
}

}

使用 Reader Writer ,将处理字符编码(CSV文件是带有编码的文本文件,如UTF

此解决方案 ,因此只有一小部分可能大的文件在内存中。


I'm trying to add a button on a web application which when clicked, downloads a CSV file (which is tiny, only about 4KB in size).

I already have the button made and a listener attached to it. The file is also ready. The only thing I need to do now is create the actual event that downloads the csv file when the button is clicked.

Assuming that "file" (which is of type File) is the name of the csv file that I'm using, and that it's ready to go, this is the method I have so far:

public void downloadCSV(HttpServletRequest _request, HttpServletResponse _response, LogFileDetailCommand cmd) throws Exception {
     ServletOutputStream outStream = _response.getOutputStream();
     _response.setContentType("text/csv");  
     _response.setHeader("Content-Disposition", "attachment; filename=data.csv");
}

I realise that I'm missing the part that writes the file to the ServletOutputStream but this is the part that I'm stuck on. I know that class ServletOutputStream extends class OutputStream (http://docs.oracle.com/javaee/5/api/javax/servlet/ServletOutputStream.html), and therefore it has inherited its "write" methods, so in an ideal world I'd like to simply say "outStream.write(file);", but I can't do that seeing as these "write" methods only have either byte, byte[] and int as the parameters, and it's not possible to pass a File into them.

What should I do to pass the file into the outStream?

(Aside note: should I add this in "_response.setContentLength();", and if so, what parameters should I put into it?)

P.s. I know that after I'm finished with outStream that I need to flush and/or close it.

解决方案

You can read the CSV file line-by-line, and write the line on the Writer of the HttpSerlvetResponse:

public class DownloadCsvServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    final File file = new File("/tmp/data.csv");

    response.setContentType("text/csv");

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

    final BufferedReader br = new BufferedReader(new FileReader(file));
    try {
        String line;
        while ((line = br.readLine()) != null) {
            response.getWriter().write(line + "\n");
        }
    } finally {
        br.close();
    }
}

}

If you use both a Reader and a Writer, the character encoding will be handled (CSV files are text files with encoding like UTF-8 for example).

This solution streams, so only a small portion of a possibly large file is in memory.

这篇关于使用Java在浏览器中下载CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆