java 使用 JSF 下载 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10451095/
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-10-31 01:05:27  来源:igfitidea点击:

Downloading a CSV file using JSF

javajsfcsvdownload

提问by yves.beutler

I don't know how to download a CSV file. The CSV will be generated at runtime. Do I need to save the file in the tomcat WEB-INF directory first? I'm using JSF 1.2.

我不知道如何下载 CSV 文件。CSV 将在运行时生成。需要先把文件保存在tomcat的WEB-INF目录下吗?我正在使用 JSF 1.2。

By the way, what's the favored JSF component for this kind of task?

顺便说一句,这种任务最喜欢的 JSF 组件是什么?



Edit (05.05.2012 - 15:53)

编辑 (05.05.2012 - 15:53)

I tried the solution BalusCstated in his first link, but if I click on my commandButton the content of the file is displayed on the webpage. Maybe there's a problem with the mimetype?

我尝试了BalusC他的第一个链接中所述的解决方案,但是如果我单击我的 commandButton,文件的内容将显示在网页上。也许有问题mimetype

xhtml-file:

xhtml 文件:

<a4j:form>
    <a4j:commandButton action="#{surveyEvaluationBean.doDataExport}" value="#{msg.srvExportButton}" />
</a4j:form>

main bean:

主豆:

    public String doDataExport() {

    try {
        export.downloadFile();  
    } catch (SurveyException e) {
        hasErrors = true;
    }
    return "";
}

export-bean:

出口豆:

public void downloadFile() throws SurveyException {

    try {

        String filename = "analysis.csv";

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();

        response.reset();
        response.setContentType("text/comma-separated-values");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

        OutputStream output = response.getOutputStream();

        // writing just sample data
        List<String> strings = new ArrayList<String>();

        strings.add("filename" + ";" + "description" + "\n");
        strings.add(filename + ";" + "this is just a test" + "\n");

        for (String s : strings) {
            output.write(s.getBytes());
        }

        output.flush();
        output.close();

        fc.responseComplete();

    } catch (IOException e) {
        throw new SurveyException("an error occurred");
    }
}


Edit (05.05.2012 - 16:27)

编辑 (05.05.2012 - 16:27)

I solved my problem. I have to use <h:commandButton>instead of <a4j:commandButton>and now it works!

我解决了我的问题。我必须使用<h:commandButton>而不是,<a4j:commandButton>现在它可以工作了!

采纳答案by BalusC

Do I need to save the file in the tomcat WEB-INF directory first?

需要先把文件保存在tomcat的WEB-INF目录下吗?

No, just write it straight to the HTTP response body as you obtain by ExternalContext#getResponseOutputStream()after having set the proper response headers which tells the browser what it's going to retrieve.

不,只需将它直接写入 HTTP 响应正文,因为您ExternalContext#getResponseOutputStream()在设置了正确的响应标头后获得了它,该标头告诉浏览器它将要检索什么。

Do the math based on the concrete examples found in the following answers:

根据以下答案中的具体示例进行数学计算:

Basically:

基本上:

List<List<Object>> csv = createItSomehow();
writeCsv(csv, ';', ec.getResponseOutputStream());


By the way, what's the favorite jsf-component for this kind of task?

顺便说一句,这种任务最喜欢的 jsf 组件是什么?

This is subjective. But anyway, we're using <p:dataExporter>to full satisfaction.

这是主观的。但无论如何,我们使用<p:dataExporter>得非常满意。

回答by choop

If you are using JSF 2 you can use primefaces.
You can take a look at that link.

如果您使用的是 JSF 2,则可以使用 primefaces。
你可以看看那个链接

If not you can do it like that:

如果没有,你可以这样做:

List<Object> report = new ArrayList<Object>(); // fill your arraylist with relevant data 
String filename = "report.csv";    
File file = new File(filename);
Writer output = new BufferedWriter(new FileWriter(file));
output.append("Column1");
output.append(",");
output.append("Column2");
output.append("\n");
//your data goes here, Replcae Object with your bean
for (Object row:report){
    output.append(row.field1);
    output.append(",");
    output.append(row.field2);
    output.append("\n");
}
output.flush();
output.close();