JAvaはadobeツールと組み合わせてPDFのオンラインプレビューを実現します.完璧な解決

3525 ワード

一.環境準備
  • 公式サイトに行ってadobe pdfサービスをダウンロードして、直接馬鹿式インストール
  • 二.コードDemo
    フロントエンド
        
          picList=(List)request.getAttribute("picList");	
           	
            
            	
            	
            		
            			
            			         
            				プレビューをクリック
            			
            			
    function forward(url){ window.location.href = url; } function quit(){ if(confirm(" ?")) { window.location.href="loginOutAction.ces"; } } // IE // adobe PDF function isAcrobatPluginInstall(path){ // firefox if (navigator.plugins && navigator.plugins.length) { for (x=0; x<navigator.plugins.length;x++) { if (navigator.plugins[x].name== 'Adobe Acrobat'){ join(path); return true; } } }else if (window.ActiveXObject){// IE for (x=2; x<10; x++){ try{ oAcro=eval_r("new ActiveXObject('PDF.PdfCtrl."+x+"');"); if (oAcro){ join(path); return true; } }catch(e) {} } try{ oAcro4=new ActiveXObject('PDF.PdfCtrl.1'); if (oAcro4){ join(path); return true; } }catch(e) {} try{ oAcro7=new ActiveXObject('AcroPDF.PDF.1'); if (oAcro7){ join(path); return true; } }catch(e) {} } alert(" Adobe PDF , , "); return; } // , function join(filePath){ if(document.getElementById("H2") != null){ document.body.removeChild(document.getElementById("H2")) return; } document.body.appendChild(document.createElement("br")); var a = document.createElement("a"); a.id="H2"; a.href="[email protected]?path="+filePath; a.innerText=" PDF"; document.body.appendChild(a) var h2 = document.getElementById("H2"); document.getElementById("H2").style.display="none"; h2.onclick=function(){ } h2.click(); }

    バックグラウンド
        /**
         *   adobe IE   ,      PDF,    IE
         * @author Cc
         */
        public void pdfNowShow() throws IOException {
        	String filePath = Function.getParameter(request, "path");
            File file = new File(filePath);
            if (!file.exists()) {
                return;
            }
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
            byte[] bs = new byte[1024];
            int len = 0;
            response.reset(); //         
            URL u = new URL("file:///" + filePath);
            String contentType = u.openConnection().getContentType();
            response.setContentType(contentType);
            //inline        
            response.setHeader("Content-Disposition", "inline;filename="+"");
            OutputStream out = response.getOutputStream();
            while ((len = br.read(bs)) > 0) {
                out.write(bs, 0, len);
            }
            out.flush();
            out.close();
            br.close();
            return;
        }
    

    これでadobe実装pdfオンラインプレビューが実現されました