Java本地化文件名 [英] Java localized filenames
问题描述
如何在java.Currently中设置本地化文件名每次我在我的应用程序中点击一个非ascii文件名的本地化文件,Windows保存对话框弹出,但它不显示文件名,如果字符集是任何东西
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-Type $ c>使用备用格式$ c>和
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屋!