We have been facing a problem that file with expected Arabic name exported from PrimeFaces has blanks (as name) instead of text followed by the extension like " .csv". File's contents are displayed in Arabic as they should be. We have Apache-Tomcat 6.0.29 server. I debugged and found that the file has the name in Arabic till the return statement (Java code given below) but couldn't debug after that. Tried the solution given in the accepted answer here Primefaces fileDownload non-english file names corrupt but it didn't work for me because I was using Firefox. We had already set the property of URIEncoding="UTF-8" in connector tag inside server.xml.
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: I tried creating file with Arabic name through plain Java and it worked.
Accepted answer in the post I shared works perfectly for most of the browsers but Firefox. To over come this, I used contentDisposition
property in p:fileDownload
tag. This syntax is a bit weird but it works. Asterisk and single quotes are part of it.
<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"/>
In normal situations, this encoded text would be passed from the bean but I have added for clarity.