/*****************处理文件名中文乱码问题 start *************************/
String agent = request.getHeader("USER-AGENT"); //获取浏览器的信息
if(agent != null && agent.toLowerCase().indexOf("firefox")>0){
//火狐浏览器自己会对URL进行一次URL转码所以区别处理
response.setHeader("Content-Disposition",
"inline; filename="+ new String(file.getName().getBytes("GB2312"),"ISO-8859-1"));
}else if(agent.toLowerCase().indexOf("safari")>0){
//苹果浏览器需要用ISO 而且文件名得用UTF-8
response.setHeader("Content-Disposition",
"inline; filename="+ new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
}else{
//其他的浏览器
response.setHeader("Content-Disposition",
"inline; filename=\""+java.net.URLEncoder.encode(file.getName(), "UTF-8"));
}
/*****************处理文件名中文乱码问题 end *************************/
response.setContentType("application/pdf");
// 如果需要下载 文件 需设置一下参数
//response.setHeader("Content-Disposition",
// "attachment; filename="+ new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
// response.setContentType("application/force-download");
如果需要下载则
把 inline 属性 换成 attachment 设置 response.setContentType("application/force-download");
另外 我做的是pdf的浏览器内查看 所以设置了 response.setContentType("application/pdf");