Java本地化文件名 [英] Java localized filenames
问题描述
如何在java.Current中设置本地化的文件名每次我单击我的应用程序中具有非ascii文件名的本地化文件时,会弹出Windows保存对话框,但是如果字符集是任何东西,则不会正确显示文件名以上ISO-88859-1。
How can i set localized filenames in java.Currently everytime i click on a localized file having a non-ascii filename in my application, the windows save dialog box pops out, but it isnt displaying the filename properly if the charset is anything above ISO-88859-1.
这是我保存文件的代码。
This is my code which is saving the file.
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, 0, read);
total += read;
}
response.flushBuffer();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
如果有人可以分享他们的想法,我会非常有帮助关于如何解决这个问题。
提前感谢
I would be very helpful if someone could share their ideas on how to resolve this issue. Thanks in advance.
推荐答案
什么gustafc说是正确的,但它不会让你在哪里是。 RFC 2231 允许您使用替代格式的非ASCII Content-键入
和 Content-Disposition
参数,但不是所有的浏览器都支持它。不幸的是,最可能的方式是忽略RFC 2183所说的内容,并使用 RFC 2047编码字在回应中:
What gustafc says is correct, but it doesn't get you where you want to be. RFC 2231 allows you to use an alternative format for non-ASCII Content-Type
and Content-Disposition
parameters, but not all browsers support it. The way that's most likely to work, unfortunately, is to ignore what RFC 2183 says and use RFC 2047 encoded-words in the response:
response.addHeader("Content-Disposition", "attachment; " +
"filename=\"" + MimeUtility.encodeWord(fileName, "utf-8", "Q") + "\"");
请注意,这可能不适用于所有浏览器。 IE的某些变体需要您将URL替换为值:
Note that this may not work for all browsers. Some variants of IE require that you URL-encode the value instead:
response.addHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(filename, "utf-8"));
这篇关于Java本地化文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!