JSF 2 Download File with Cyrillic Name

I have a data file and a button to download the selected file.

If the file name is in Cyrillic characters, the browser says "Unknown file type", Example: I have the file "addad .png" and I click download the browser response enter image description here

there is my loading method

public void download(Files file) { try { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseHeader("Content-Type", "application/x-download"); externalContext.setResponseHeader("Content-Length", file.getFileContent().length+""); externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + file.getFilename() + "\""); externalContext.getResponseOutputStream().write(file.getFileContent()); facesContext.responseComplete(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } } 

im pretty sure i need to encode the file name in UTF-8 but i dont know how ... please help.

+4
source share
1 answer

Use URLEncoder .

 URLEncoder.encode(file.getFileName(), "UTF-8") 

Note that this is already implicitly performed by OmniFaces Faces#sendFile() . Therefore, if you are already using OmniFaces, you can simply use it directly.

Part of the "unknown file type" is caused by the use of an unsupported content type. You must use the correct content type, which is image/png for PNG files. You can use ExternalContext#getMimeType() to get the correct content type based on the file name. This is also already implicitly done using Faces#sendFile() .

+6
source

Source: https://habr.com/ru/post/1482557/


All Articles