test

博客园 首页 新随笔 联系 订阅 管理
response.reset();
response.setContentType("application/x-download");//下载
//response.setContentType("application/pdf;charset=UTF-8");//pdf预览
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//文件名兼容火狐
String agent =((HttpServletRequest)request).getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")){
    pdfFileName= URLEncoder.encode(pdfFileName,"utf-8");
    pdfFileName=pdfFileName.replaceAll("\\+",  " ");
}
else{
    if (agent.contains("MSIE")||agent.contains("Trident")) {
        pdfFileName = URLEncoder.encode(pdfFileName, "UTF-8");
    }else{
        pdfFileName =  new String( pdfFileName.getBytes("UTF-8"), "ISO-8859-1" );
    }
}


((HttpServletResponse)response).addHeader("Content-Disposition", "attachment;filename=\""+pdfFileName+"\"");
((HttpServletResponse)response).addHeader("Content-Length", "" + fileBuff.length);
OutputStream os = null;
try{
    os = new BufferedOutputStream(response.getOutputStream());
    os.write(fileBuff);
}
catch(Exception e){}
finally{
    if(os!=null)
        os.close();
}

 

posted on 2019-12-11 10:20  testgogogo  阅读(1374)  评论(0)    收藏  举报