今天遇到一个奇葩需求,文件后缀名是中文的.
例子:文件名.后缀名
文件名不会乱码,但是后缀名会乱码,求大神指点..
后台是java的.
而且就ie8不行,使用其他浏览器,以及高版本ie显示正常.
@RequestMapping("/download")
public HttpServletResponse download(String fileName, HttpServletRequest request,
HttpServletResponse response,String fileUrl) throws Exception {
try {
//获得文件
File file = new File(fileUrl);
//获取文件名
fileName = URLDecoder.decode(fileName,"utf-8");
//获得文件后缀名
String ext = fileName.substring(fileName.indexOf(".")+1).toUpperCase();
//以流的形式下载文件
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
//设置response header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}