Primefaces fileDownload non-english file names corrupt

烂漫一生 提交于 2019-12-01 17:19:16
Andrew

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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!