我正在写一个简单的文件下载servlet,我无法获得正确的文件名。尝试URLEncoding和MimeEncoding在现有的答案中看到的文件名,但没有一个工作。Java servlet下载文件名特殊字符
以下片段中的fileData对象包含MIME类型,字节[]内容和文件名,至少需要ISO-8859-2字符集,ISO-8859-1是不够的。
如何让我的浏览器正确显示下载的文件名?
这里是文件名的例子:árvíztűrőtükörfúrógép.xls并且它导致:árvíztqrptükörfúrógép.xls
protected void renderMergedOutputModel(Map model, HttpServletRequest req, HttpServletResponse res) throws Exception {
RateDocument fileData = (RateDocument) model.get("command.retval");
OutputStream out = res.getOutputStream();
if(fileData != null) {
res.setContentType(fileData.getMime());
String enc = "utf-8"; //tried also: ISO-8859-2
String encodedFileName = fileData.getName();
// also tried URLencoding and mime encoding this filename without success
res.setCharacterEncoding(enc); //tried with and without this
res.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);
res.setContentLength(fileData.getBody().length);
out.write(fileData.getBody());
} else {
res.setContentType("text/html");
out.write("<html><head></head><body>Error downloading file</body></html>"
.getBytes(res.getCharacterEncoding()));
}
out.flush();
}
请给,你会得到什么,而不是文件名怎么看一些例子。 – BalusC 2011-03-16 12:53:55
árvíztűrőtükörfúrógép.xls - >árvíztqrptükörfúrógép.xls – jabal 2011-03-16 13:15:49
是的,你是对的。这两个字符在ISO-8859-2中仅在ISO-8859-2中不存在,导致每个匈牙利开发者都遇到许多问题.. :-) – jabal 2011-03-16 13:28:32