代码
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") < 0){
if(request.getHeader("User-Agent").toUpperCase().indexOf("EDGE")> 0){
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8"));
}else{
response.setHeader("Content-Disposition", "attachment; filename="+ new String(gridFSDBFile.getFilename().getBytes("GBK"), "ISO8859-1"));
}
}else{
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8"));
}
应用
@Override
public void processDownGridFs(String id, HttpServletResponse response) {
if(id != null && id.length() == 24) {
GridFSDBFile gridFSDBFile = gridFsDao.find(id);
if (gridFSDBFile != null) {
try{
OutputStream sos = response.getOutputStream();
response.setContentType("application/octet-stream");
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") < 0){
if(request.getHeader("User-Agent").toUpperCase().indexOf("EDGE")> 0){
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8"));
}else{
response.setHeader("Content-Disposition", "attachment; filename="+ new String(gridFSDBFile.getFilename().getBytes("GBK"), "ISO8859-1"));
}
}else{
response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8"));
}
gridFSDBFile.writeTo(sos);
sos.flush();
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}