if(request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0){// IE
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = fileName.replace("+", "%20");// 处理空格变“+”的问题
}else{// FF
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
}
response.setContentType("application/octet-stream;");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
response.setHeader("Content-Length", String.valueOf(file.length()));
FileInputStream fis = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
FileCopyUtils.copy(fis, out);
IE下会自动decode所以要encode,encode后空格会变成+号,所以要转成正常的空格%20,而火狐是不会decode,但名字要转成iso-8859-1,最后文件名要用引号引用起来,不然遇到空格会把后面的截断。