java 文件字节流(pdf)预览

 response.reset();
            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", "inline;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();
            }

前端使用jquery.media.js,如果后端有http的下载地址,可以不需要用流的方式预览

猜你喜欢

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