如何在
java.Current中设置本地化文件名每次我点击我的应用程序中具有非ascii文件名的本地化文件时,会弹出
Windows保存对话框,但如果字符集是ISO-以上的任何内容,则它不能正确显示文件名88859-1.
InputStream inputStream = null; try { response.resetBuffer(); response.setContentType(fileStream.getContentType()); response.setContentLength((int) fileStream.getContentLength()); response.addHeader("Content-Disposition","attachment;filename=\"" + fileName + "\""); ServletOutputStream stream = response.getOutputStream(); byte[] buffer = new byte[1024]; int read = 0; int total = 0; inputStream = fileStream.getInputStream(); while ((read = inputStream.read(buffer)) > 0) { stream.write(buffer,read); total += read; } response.flushBuffer(); } finally { if (inputStream != null) { inputStream.close(); } }
解决方法
古斯塔夫说的是正确的,但它并没有让你到达你想成为的地方.
RFC 2231允许您为非ASCII Content-Type和Content-Disposition参数使用替代格式,但并非所有浏览器都支持它.遗憾的是,最可能工作的方式是忽略RFC 2183所说的内容并在响应中使用
RFC 2047 encoded-words:
response.addHeader("Content-Disposition","attachment; " + "filename=\"" + MimeUtility.encodeWord(fileName,"utf-8","Q") + "\"");
请注意,这可能不适用于所有浏览器. IE的某些变体要求您对值进行URL编码:
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename,"utf-8"));