Nur*_*jan 6 filenames download non-english primefaces
我使用的是Primefaces 3.2.我在使用primefaces fileDownload时遇到了问题.我可以上传文件并在服务器上保留他们的非英文名称(在我的情况下,这是俄语).但是,当我使用p:fileDownload下载上传的文件时,我不能使用俄语字母,因为它们会被破坏.似乎DefaultStreamedContent类构造函数只接受拉丁字母.我根据primefaces网站上的展示做了一切,如下所示.
public FileDownloadController() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
Run Code Online (Sandbox Code Playgroud)
我有什么想法可以解决我的问题?
提前致谢.
小智 13
这在即将推出的PrimeFaces 6.2中得到修复,但对于早期版本,需要应用以下修复.在下面评论的链接中,发布了对PrimeFaces问题的引用,其中包含以下修复适用于Chrome,IE和Opera但不适用于FireFox的信息(未提及版本,也未提及'Edge')
解决方法
尝试以application/x-www-form-urlencodedMIME格式(URLEncoder)对文件名进行编码.
例:
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;
}
Run Code Online (Sandbox Code Playgroud)