Javaweb中利用pdf.js顯示oracle數(shù)據(jù)庫中Base64編碼轉(zhuǎn)為的PDF

1.下載Jar包 sun.misc.BASE64Decoder.jar

有的說要下載sun.misc.BASE64Encoder和sun.misc.BASE64Decoder包但是發(fā)現(xiàn)BASE64Decoder中就存在encoder類
不過sun這個包并沒有在java api中出現(xiàn)過所以其實(shí)不安全的可以采用
org.apache.commons.codec.binary.Base64

2.將數(shù)據(jù)庫中存儲的PDF二進(jìn)制文件 轉(zhuǎn)為PDF文件放在項(xiàng)目目錄

    public String downloadWsbrPdf(){
        optResult = new JsonActionResult();
        BufferedInputStream bin = null;
        FileOutputStream fout = null;
        BufferedOutputStream bout = null;
        try {
            String wsnr="";
            String realpath=ServletActionContext.getServletContext().getRealPath("/");
            System.out.println("CourtRequestAction.downloadWsbrPdf bdhm:"+bdhm);
            CourtRequestWs courtRequestWs=new CourtRequestWs();
            List<CourtRequestWs> wss=courtRequestWsService.getListByClause(" BDHM='"+bdhm+"'",null);
            if(wss!=null && wss.size()>0){
                courtRequestWs=wss.get(0);
                //updateWsnr(courtRequestWs);
            }else{
                optResult.setErrorMsg("沒有文書!");
            }
            //獲取ws表的證明
            if(courtRequestWs.getWsnr()!=null){
                wsnr=new String(courtRequestWs.getWsnr(),"GBK");
            }
            //進(jìn)行base64解碼
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes=decoder.decodeBuffer(wsnr);
            //創(chuàng)建一個將bytes作為其緩沖區(qū)的ByteArrayInputStream對象
            ByteArrayInputStream inputStream=new ByteArrayInputStream(bytes);
            //創(chuàng)建從底層輸入流中讀取數(shù)據(jù)的緩沖輸入流對象
            bin=new BufferedInputStream(inputStream);
            String fileName=realpath+bdhm+".pdf";
            //指定輸出的文件
            File file =new File(fileName);
            //創(chuàng)建到指定文件的輸出流
            
            fout=new FileOutputStream(file);
            //為文件輸出流對接緩存輸出流對象
            bout=new BufferedOutputStream(fout);
            byte[] buffers=new byte[1024];
            int len=bin.read(buffers);
            while (len!=-1) {
                bout.write(buffers, 0, len);
                len=bin.read(buffers);
            }
            //刷新輸出流并強(qiáng)制寫出所有的緩存的輸出字節(jié)
            bout.flush();
            
        } catch (Exception e) {
            optResult.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }finally{
            try {
                bin.close();
                fout.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return SUCCESS;
        
    }
public String deletepdf(){
        System.out.println("CourtRequestAction.deletepdf bdhm:"+bdhm);
        optResult = new JsonActionResult();
        try {
            String realpath=ServletActionContext.getServletContext().getRealPath("/");
            String deleteFile=realpath+bdhm+".pdf";
            File file=new File(deleteFile);
            if(!file.exists()){
                System.out.println("刪除文件失敗,文件不存在");
                
            }else{
                if(file.isFile()){
                    //如果要刪除的是文件
                    if(file.delete()){
                        System.out.println("刪除為預(yù)覽生成的文件成功!");
                    }
                }
            }
            
        } catch (Exception e) {
            optResult.setErrorMsg(e.getMessage());
        }
        
        return SUCCESS;
        
    }

3.前臺調(diào)用pdf.js

function viewWenshu(){
        var selDate = $("#courtRequestList").cusgrid("getSelected");
        if(selDate){
            var bdhm=selDate.bdhm;
        $.get("<%=basePath%>courtrequest/downloadWsbrPdf.action",{bdhm:bdhm},function(data){
            if(data.success){
                $("#courtPdf").cusdialog({
                    width:"95%",
                    height:"95%",
                     title:"證明文件",
                     content:"<iframe id='pdfcontent' src='<%=basePath%>js/pdfjs/web/viewer.html?AUTHID="+$("#authId").val()+"&file=<%=basePath%>"+bdhm+".pdf' width='100%' height='100%' frameborder='no' scrolling='no'></iframe>",
                     hideBtn:true,
                     onBeforeClose:function(){
                        //關(guān)閉時(shí)刪除產(chǎn)生的pdf文件
                        $.get("<%=basePath%>courtrequest/deletepdf.action",{bdhm:bdhm},function(data){
                            if(data.success){
                                console.log("刪除為預(yù)覽生成的pdf文件成功");
                            }
                            
                        });
                        
                    }

                });
                //window.open("<%=basePath%>js/pdfjs/web/viewer.html?AUTHID="+$("#authId").val()+"&file=<%=basePath%>"+bdhm+".pdf");
            }else{
                $.cusalert({title:'提示',content:data.errorMsg,type:'DEFAULT'});
            }
        })
        }else{
            $.cusalert({title:'提示',content:'請選擇一條數(shù)據(jù)。',type:'DEFAULT'});
        }
    }

pdf.js文件地址:https://git.oschina.net/Jicklin/pdfjs.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容