前端
<a href="${ctx}/oa/admin/contract/pdfView?id=${oaContract.id}" target="_blank">
<i class="icon-check-c"></i><font style="font-weight:bold;color:green">阅览合同书</font></a>
后台
/**
* 在线阅读pdf文件
*/
@ResponseBody
@RequestMapping(value = "/oa/admin/contract/pdfView", method = RequestMethod.GET)
public void contractPDFView(@RequestParam("id") Integer id,HttpServletResponse response)
throws Exception {
OAAdminContract contract = jpaDao.get(OAAdminContract.class, id);
String fileName = contract.getFileName();
String filePath = contractPath+fileName;
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found! 您还没上传文件!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
// 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1"));
// 文件名应该编码成UTF-8
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}