Java 通过 Rest 下载 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21876286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:12:39  来源:igfitidea点击:

Download CSV file via Rest

javarestcsvopencsv

提问by Spring

Using OpenCSV, I can successfully create a CSV file on disc, but what I really need is to allow users download the CSV with a download button, I don't need to save on disk, just download. Any ideas?

使用 OpenCSV,我可以在光盘上成功创建 CSV 文件,但我真正需要的是允许用户使用下载按钮下载 CSV,我不需要保存在磁盘上,只需下载即可。有任何想法吗?

@GET
@Path("/downloadCsv")
public Object downloadCsv() {
        CSVWriter writer;
        FileWriter wr; 
      //Or should I use outputstream here?   
        wr= new FileWriter("MyFile.csv");
        writer = new CSVWriter(wr,',');
        for (Asset elem: assets) {
            writer.writeNext(elem.toStringArray());
        }
        writer.close();


}

EDIT: I do NOT want to save/read file on disc EVER

编辑:我不想在光盘上保存/读取文件

采纳答案by tom

To force "save as", you need to set the content disposition HTTP header in the response. It should look like this:

要强制“另存为”,您需要在响应中设置内容处置 HTTP 标头。它应该是这样的:

Content-Disposition: attachment; filename="whatever.csv"

It looks like you're using JAX-RS. This questionshows how to set the header. You can either write the CSV to the HTTP response stream and set the header there or return a Responseobject like so:

看起来您正在使用 JAX-RS。这个问题显示了如何设置标题。您可以将 CSV 写入 HTTP 响应流并在那里设置标头或返回一个Response对象,如下所示:

return Response.ok(myCsvText).header("Content-Disposition", "attachment; filename=" + fileName).build();

You do not need to write to a Fileobject in the middle of this process so can avoid writing to disk.

您不需要File在此过程中写入对象,因此可以避免写入磁盘。

回答by AlexR

First, you code cannot be compiled, right? Method downloadCsv()declares return type Objectbut does not return anything.

首先,您的代码无法编译,对吗?方法downloadCsv()声明返回类型Object但不返回任何内容。

I'd change the declaration to String downloadCsv()and return the content of CSV as string. To do this use StringWriterinstead of FileWriterand then say return wr.toString().

我将声明更改为String downloadCsv()并将 CSV 的内容作为字符串返回。要做到这一点,请使用StringWriter而不是FileWriter然后说return wr.toString().

The only thing that is missing here is content type. You annotate your method as @Produces({"text/csv"}).

这里唯一缺少的是内容类型。您将方法注释为@Produces({"text/csv"}).

I think, that's it.

我想,就是这样。

回答by Reeebuuk

            response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
            response.setContentType("text/csv");
            OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), "UTF-8");

            List<String[]> result = iSmsExportService.csvExport(columnNames);
            CSVWriter csvWriter = new CSVWriter(osw, ';');
            csvWriter.writeAll(result);
            csvWriter.flush();
            csvWriter.close();

Downloading of CSV file has started after this.

此后开始下载 CSV 文件。