gpt4 book ai didi

primefaces - Primefaces文件下载损坏的非英语文件名

转载 作者:行者123 更新时间:2023-12-04 13:33:49 25 4
gpt4 key购买 nike

我正在使用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");
}

有什么想法可以解决我的问题吗?

提前致谢。

最佳答案

这是fixed in the upcoming PrimeFaces 6.2,但是对于早期版本,需要应用以下修复程序。在下面评论的链接中,张贴了对PrimeFaces问题的引用,其中包含以下信息适用于Chrome,IE和Opera,但不适用于FireFox(未提及版本,也未提及“Edge”)

解决方法

尝试使用application/x-www-form-urlencoded MIME格式(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;
}

关于primefaces - Primefaces文件下载损坏的非英语文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com