【问题标题】:Downloading a CSV file using JSF使用 JSF 下载 CSV 文件
【发布时间】:2012-05-04 14:46:17
【问题描述】:

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

顺便问一下,这种任务最受青睐的 JSF 组件是什么?


编辑 (05.05.2012 - 15:53)

我尝试了他的第一个link 中所述的解决方案BalusC,但是如果我点击我的commandButton,文件的内容就会显示在网页上。 mimetype可能有问题?

xhtml 文件:

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

主豆:

    public String doDataExport() {

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

出口豆:

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");
    }
}

编辑 (05.05.2012 - 16:27)

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

【问题讨论】:

    标签: java jsf csv download


    【解决方案1】:

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

    不,只需在设置正确的响应标头后将其直接写入 HTTP 响应正文,就像您通过 ExternalContext#getResponseOutputStream() 获得的那样,告诉浏览器它将检索什么。

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

    基本上:

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

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

    这是主观的。但无论如何,我们使用&lt;p:dataExporter&gt; 非常满意。

    【讨论】:

    • 我不认为我可以使用PrimeFaces,因为他们不支持JSF 1.2,因为PrimeFaces 2.0 我是对的?
    • 没错,古老的 JSF 1.x 已经接近尾声,因此您不应该期望新的组件库支持它。只需继续手动流式传输即可。您只需要通过ExternalContext#getResponse() 获取原始HttpServletResponse,然后调用它的getOutputStream(),但这已经在第一个链接中进行了概述。或者,当然,升级到 JSF 2。与 JSF 1.2 相比,它提供了许多优势。
    • 我知道 jsf 1.2 的缺点,但目前我们无法升级到更高的级别。我试图理解你的第一个链接,但它不起作用。我使用了我在网上找到的 mimetype text/comma-separated-values,但是浏览器(IE、FF、Chrome)不能正确解释它。
    • 如果我使用a4j:components 那么它不会工作,但现在我改为&lt;h:commandButton&gt; 并且它可以正常工作。感谢 BalusC 的另一个好答案!
    • 不客气。确实,您不能通过 ajax 下载文件。这在第一个链接中也已经提到。
    【解决方案2】:

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

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

    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();
    

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2020-10-28
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多