从 Firefox 上的 PrimeFaces 导出具有阿拉伯名称的文件

Export file with Arabic name from PrimeFaces on Firefox

我们一直面临一个问题,即从 PrimeFaces 导出的具有预期阿拉伯名称的文件包含空白(作为名称),而不是文本后跟“.csv”等扩展名。文件内容按应有的方式以阿拉伯语显示。我们有 Apache-Tomcat 6.0.29 服务器。我调试并发现该文件在 return 语句(下面给出的 Java 代码)之前具有阿拉伯语名称,但之后无法调试。尝试了此处接受的答案中给出的解决方案 Primefaces fileDownload non-english file names corrupt 但它对我不起作用,因为我使用的是 Firefox。我们已经在 [=28] 内的 connector 标签中设置了 URIEncoding="UTF-8" 的 属性 =].

XHTML:

<p:commandLink id="export" ajax="false" > Export
<p:fileDownload value="#{groupView.export}"/>
</p:commandLink>

Java:

 public StreamedContent getExport() {

    String content = "Any text in Arabic or English";

    // send CSV file to browser
    InputStream is;
    StreamedContent file = null;
    try {
        is = new ByteArrayInputStream(content.getBytes("UTF-8"));
        file = new DefaultStreamedContent(is, "application/csv", "المشاركات.csv","UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return file; //FILE NAME IS IN ARABIC TILL HERE
    }

PS:我尝试通过纯 Java 创建具有阿拉伯名称的文件并且成功了。

我分享的 post 中接受的答案适用于除 Firefox 之外的大多数浏览器。为了克服这个问题,我在 p:fileDownload 标签中使用了 contentDisposition 属性。这种语法有点奇怪,但它有效。星号和单引号是其中的一部分。

<p:fileDownload value="#{groupView.export}" contentDisposition="inline; filename*=utf-8''%D8%A7%D9%84%D9%85%D8%B4%D8%A7%D8%B1%D9%83%D8%A7%D8%AA.csv"/>

在正常情况下,此编码文本将从 bean 传递,但为了清楚起见我添加了。