java字节流浏览器下载

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();
}

猜你喜欢

转载自www.cnblogs.com/kkvt/p/12020921.html
今日推荐