pdf在线预览功能

1 篇文章 0 订阅
1 篇文章 0 订阅
1、contentfile 是webroot目录下的文件夹 待预览的文件上传到此文件夹。
2、Specificate 存放预览文件信息的实体类(表)。
3、spath + "\\" + specificate.getTitle(); 路径加 文件名取到文件。

public void preview() {

String spath = ServletActionContext.getServletContext().getRealPath("")
+ "\\contentfile\\";
specificate = (Specificate) baseService.getById(Specificate.class, id);
String fileName = specificate.getTitle();
String fileNameWithPath = spath + "\\" + specificate.getTitle();
log.info("文件名=" + fileName);
// File file = new File(fileName);

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 = "moren.pdf";
fileNameWithPath = spath + "\\" + fileName;
}
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) {
log.error("打开失败,文件路径为:" + fileNameWithPath, e);
e.printStackTrace();
// return this.ajaxText("打开失败");
}
// return "";
}


jsp页面
<input type="button" id="<s:property value='spId'/>"														class="btn btn-success" value="预览"	onclick="viewPDF(this)" />

script

<script type="text/javascript">

function viewPDF(cur){
// alert($(cur).attr('id'));
var fileid = $(cur).attr('id');
var strURL = "preview.action?id="+fileid;
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);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值