1.下載Jar包 sun.misc.BASE64Decoder.jar
有的說要下載sun.misc.BASE64Encoder和sun.misc.BASE64Decoder包
但是發(fā)現(xiàn)BASE64Decoder
中就存在encoder類
不過sun這個包并沒有在java api
中出現(xiàn)過所以其實不安全的可以采用
org.apache.commons.codec.binary.Base64
類
2.將數(shù)據(jù)庫中存儲的PDF二進制文件 轉(zhuǎn)為PDF文件放在項目目錄
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");
}
//進行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);
}
//刷新輸出流并強制寫出所有的緩存的輸出字節(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)閉時刪除產(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