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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM