1.下载Jar包 sun.misc.BASE64Decoder.jar
有的说要下载sun.misc.BASE64Encoder和sun.misc.BASE64Decoder包
但是发现BASE64Decoder
中就存在encoder类
不过sun这个包并没有在java api
中出现过所以其实不安全的可以采用
org.apache.commons.codec.binary.Base64
类
2.将数据库中存储的PDF二进制文件 转为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 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);
//创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
ByteArrayInputStream inputStream=new ByteArrayInputStream(bytes);
//创建从底层输入流中读取数据的缓冲输入流对象
bin=new BufferedInputStream(inputStream);
String fileName=realpath+bdhm+".pdf";
//指定输出的文件
File file =new File(fileName);
//创建到指定文件的输出流
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);
}
//刷新输出流并强制写出所有的缓存的输出字节
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("删除为预览生成的文件成功!");
}
}
}
} catch (Exception e) {
optResult.setErrorMsg(e.getMessage());
}
return SUCCESS;
}
3.前台调用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:"",
hideBtn:true,
onBeforeClose:function(){
//关闭时删除产生的pdf文件
$.get("<%=basePath%>courtrequest/deletepdf.action",{bdhm:bdhm},function(data){
if(data.success){
console.log("删除为预览生成的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:'请选择一条数据。',type:'DEFAULT'});
}
}
pdf.js文件地址:https://git.oschina.net/Jicklin/pdfjs.git