java pdf预览_PDF在线预览(java实现)

String fileName = getRequest().getParameter("filename"); //如路径 C:\\Users\\zj\\AppData\\Local\\Microsoft\\Windows\\Temporary Internet Files\\Content.IE5\\1347500646246.pdf

String location = getRequest().getParameter("location"); //如路径 attachments/2012/09/13/FDCD7438-85BB-4008-8F67-9E8C0C82D6FB.pdf

String basePath = SystemParamConfigUtil.getParamValueByParam("filePath"); //读取配置文件附件存储路径//如路径 d:/data/file/oadoc/

String fileNameWithPath =basePath+location;

try {

// 转码(UTF-8-->GB2312),现在环境下的编码是UTF-8,但服务器操作系统的编码是GB2312

if(fileName!=null&&fileName.trim().length()>0){

fileName = URLEncoder.encode(fileName, "GB2312");

fileName = URLDecoder.decode(fileName, "ISO8859-1");

}else{

fileName = "a."+FileUploadUtils.getExtension(location).toLowerCase();

}

File file = new File(fileNameWithPath);

FileInputStream fileinputstream = new FileInputStream(file);

long l = file.length();

int k = 0;

byte abyte0[] = new byte[65000];

getResponse().setContentType("application/pdf");

getResponse().setContentLength((int) l);

getResponse().setHeader("Content-Disposition", "inline; filename="+ fileName);

while ((long) k < l) {

int j;

j = fileinputstream.read(abyte0, 0, 65000);

k += j;

getResponse().getOutputStream().write(abyte0, 0, j);

}

fileinputstream.close();

} catch (IOException e) {

logger.error("打开失败,文件路径为:"+fileNameWithPath, e);

e.printStackTrace();

}

js:

/**

* 预览PDF

*

*/

function viewPDF(fileLocation){

var filename =httpDownFileLocal(fileLocation);

var strURL = "attachment_viewPdf.do?filename="+filename+"&location="+fileLocation;

var sheight = screen.height-70;

var swidth = screen.width-10;

var winoption="left=0,top=0,height="+sheight+",width="+swidth+",toolbar=yes,menubar=yes,

location=yes,status=yes,scrollbars=yes,resizable=yes";

var tmp=window.open(strURL,'',winoption);

}

/**

* 下载文件到IE本地缓存目录下

*

*/

function httpDownFileLocal(filepathAndName){

var filepath = filepathAndName;

var WshShell = new ActiveXObject("WScript.Shell");

//缓存目录

var keyValue = WshShell.RegRead('HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Cache\\Paths\\Directory');

var PostRecvImpl = new ActiveXObject("ASPCom.PostRecv");

var extName = filepath.substring(filepath.lastIndexOf('.'));

var fileName = new Date().getTime() + extName;

fileName = keyValue+"\\"+fileName;

fileName = fileName.replace(/\\/gm,'\\\\');

PostRecvImpl.HTTPDownloadFile(fileName,filepath);

return fileName;

}

Adobe--设置:“编辑”--“首选项”--“因特网”--把三个复选框打√即可...

详见:附件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值