6

I am using Primefaces 3.2. I've got problems with using primefaces fileDownload. I can upload the files and keep their non-english name on the server (in my case this is Russian). However, when I use p:fileDownload to download the uploaded files I cannot use Russian letters since they get corrupt. It seems that the DefaultStreamedContent class constructor accepts only Latin letters. I am doing everything according to the showcase on the primefaces website as shown below.

public FileDownloadController() {          
    InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");  
    file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");  
}

Any ideas how I can solve my problem?

Thanks, in advance.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • I'm swamped, but can you link a file as an example? I ripped this servlet apart 4 months ago and I'd be happy to run it through the debugger if I have something to use. – Daniel B. Chapman May 02 '12 at 07:17
  • You mean the file I used? How can I link it here? I used .docx file with russian name. If it's not difficult for you, you can create any .doc or .docx file and give it a name in russian "Тестовый файл" (Test file). – Nurjan May 03 '12 at 03:34
  • 1
    Sorry for the delay: I wasn't able to recreate, however here's where you want to look: http://code.google.com/p/primefaces/source/browse/primefaces/trunk/src/main/java/org/primefaces/webapp/MultipartRequest.java Basically it is built on top of ApacheFileUpload and it is using that method (parseRequest) to do it. Also, make sure that your char-set is correct and it isn't streaming to ASCII or something silly when it saves. – Daniel B. Chapman May 09 '12 at 16:27

1 Answers1

13

This is fixed in the upcoming PrimeFaces 6.2, but for earlier versions the fix below needs to be applied. In a link in the comments below a reference to a PrimeFaces issue was posted which contains info that the fix below does work for Chrome, IE and Opera but not for FireFox (no version mentioned, nor is 'Edge' mentioned)

Workaround

Try to encode your file name in application/x-www-form-urlencoded MIME format (URLEncoder).

Example:

public StreamedContent getFileDown () {
        // Get current position in file table
        this.currentPosition();
        attachments = getAttachments();
        Attachment a = getAttachmentByPosition( pos, attachments );

        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        // Detecting MIME type
        String mimeType = fileNameMap.getContentTypeFor(a.getAttachmentName());
        String escapedFilename = "Unrecognized!!!";
        try {
            // Encoding
            escapedFilename = URLEncoder.encode(a.getAttachmentName(), "UTF-8").replaceAll(
                    "\\+", "%20");
        } catch (UnsupportedEncodingException e1) {         
            e1.printStackTrace();
        }
        // Preparing streamed content
        fileDown = new DefaultStreamedContent( new ByteArrayInputStream( a.getAttachment() ),
                mimeType, escapedFilename);
        return fileDown;
    }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Andrew
  • 186
  • 2
  • 7
  • 1
    Yes, it works fine for me with russian and ukrainian file names. – Andrew Feb 27 '13 at 19:55
  • Thanks, it worked for me, however the displayed name which I get through the name property of the DefaultStreamedContent gets also url encoded. – Nurjan Mar 01 '13 at 11:26
  • @Nurzhan Do not quite understand what you meant. For displaying file names on the page, I get the name of the file directly from `Attachment` object. If you need to decode this format use [URLDecoder](http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLDecoder.html) – Andrew Mar 04 '13 at 20:27
  • 1
    Hopefully will be fixed in PF eventually: https://github.com/primefaces/primefaces/issues/149 – Vsevolod Golovanov Aug 21 '15 at 11:31
  • In this link to the PrimeFaces Github is additional information: It does not seem to work on FireFox (no versions mentioned, so your milage might vary). But this issue is marked as a duplicate of another issue https://github.com/primefaces/primefaces/issues/1312 which states it will be fixed in the upcoming 6.2 release – Kukeltje Nov 19 '17 at 10:36