不同服务器的文件名错误
Wrong file name in different servers
我有一个允许用户下载一些文件的应用程序。我 运行 它在 VMware vFabric tc Server Developer Edition v2.9(STS 中的默认服务器)上运行,一切正常。但是现在我的服务器是 GlassFish,所以文件名不对。
例如 - 30-60 от 19.01.2015.txt
Firefox:30-60_(没有扩展名)
Chrome: 30-60_ _19.01.2015.txt
怎么了?代码:
response.setHeader(
"Content-disposition",
"attachment;filename="
+ BufferUtil.getEncoded(distributionService.get(id)
.getTitle(), request) + ".txt");
response.setContentType("text/xml, text/plain");
BufferUtil.getEncoded:
public static String getEncoded(String text, HttpServletRequest request) {
String userAgent = request.getHeader("user-agent");
boolean isInternetExplorer = (userAgent.indexOf("MSIE") > -1);
text=text.replace(' ', '_');
try {
byte[] fileNameBytes = text
.getBytes((isInternetExplorer) ? ("windows-1250")
: ("utf-8"));
String dispositionFileName = "";
for (byte b : fileNameBytes) {
dispositionFileName += (char) (b & 0xff);
}
return dispositionFileName;
} catch (UnsupportedEncodingException ence) {
ence.printStackTrace();
return null;
}
}
请尝试:
java.net.URLEncoder.encode(distributionService.get(id).getTitle() + ".txt", "UTF-8")
而不是
BufferUtil.getEncoded(distributionService.get(id)
.getTitle(), request) + ".txt");
我有一个允许用户下载一些文件的应用程序。我 运行 它在 VMware vFabric tc Server Developer Edition v2.9(STS 中的默认服务器)上运行,一切正常。但是现在我的服务器是 GlassFish,所以文件名不对。 例如 - 30-60 от 19.01.2015.txt
Firefox:30-60_(没有扩展名)
Chrome: 30-60_ _19.01.2015.txt
怎么了?代码:
response.setHeader(
"Content-disposition",
"attachment;filename="
+ BufferUtil.getEncoded(distributionService.get(id)
.getTitle(), request) + ".txt");
response.setContentType("text/xml, text/plain");
BufferUtil.getEncoded:
public static String getEncoded(String text, HttpServletRequest request) {
String userAgent = request.getHeader("user-agent");
boolean isInternetExplorer = (userAgent.indexOf("MSIE") > -1);
text=text.replace(' ', '_');
try {
byte[] fileNameBytes = text
.getBytes((isInternetExplorer) ? ("windows-1250")
: ("utf-8"));
String dispositionFileName = "";
for (byte b : fileNameBytes) {
dispositionFileName += (char) (b & 0xff);
}
return dispositionFileName;
} catch (UnsupportedEncodingException ence) {
ence.printStackTrace();
return null;
}
}
请尝试:
java.net.URLEncoder.encode(distributionService.get(id).getTitle() + ".txt", "UTF-8")
而不是
BufferUtil.getEncoded(distributionService.get(id)
.getTitle(), request) + ".txt");