0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MNH
  • 13
  • 5
  • Click the link to the other question. It fails, please correct it. And please try to make an [mcve]. Saves others work in trying to reproduce – Kukeltje Nov 19 '17 at 07:27
  • Edited. Link is working now :) – MNH Nov 19 '17 at 07:33
  • I will change the code snippets – MNH Nov 19 '17 at 07:36
  • I have edited the code. Just copy paste in the existing files will do. – MNH Nov 19 '17 at 08:15
  • You still **don't** url encode the name like stated in the accepted answer... Why? – Kukeltje Nov 19 '17 at 08:27
  • I tried that too. But that didn't work – MNH Nov 19 '17 at 08:34
  • Encoding changed the name from blanks to something like B@6A451c – MNH Nov 19 '17 at 08:37
  • Then you did something wrong... that is the object hash! – Kukeltje Nov 19 '17 at 08:38
  • See how you question is _not good_? I have to ask all sorts of clarifications that you could and **should** have added upfront. The last statements are the most important **and effectively your 'quedtion'**: URLEndoding a filename fails and results in.... – Kukeltje Nov 19 '17 at 08:45
  • `String fileName = URLEncoder.encode("المشاركات", "utf-8").replaceAll("\\+", "%20"); file = new DefaultStreamedContent(is, "application/csv", fileName+ ".csv","UTF-8");` I tried this and resulted in percent encoding. B@6A451c is for some other solution I tried (my mistake). – MNH Nov 19 '17 at 09:07
  • Thanks for sending down the wrong path... Did you fully read oher Q/A including the comments and everything in the links that is in the comments? And all relevant links in there? Tried the latest PF snapshot from source in github – Kukeltje Nov 19 '17 at 09:08
  • Sorry for that. I did read them and many others before posting this. Tried all sorts of solutions. – MNH Nov 19 '17 at 09:11
  • I edited the commet above a little, please read again. There is lots more info in there that you also don't talk about. Cheers... that's all from me... good luck further – Kukeltje Nov 19 '17 at 09:13
  • Next time, read, read, read, like I did for you. You are welcome – Kukeltje Nov 21 '17 at 18:28
  • Thanks! I must have been half asleep while reading – MNH Nov 22 '17 at 06:48
  • 1
    Possible duplicate of [Primefaces fileDownload non-english file names corrupt](https://stackoverflow.com/questions/10407156/primefaces-filedownload-non-english-file-names-corrupt) – Kukeltje Nov 22 '17 at 08:21

1 Answers1

0

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.

MNH
  • 13
  • 5