java 实现pdf文件的预览

点击连接,实现现在预览pdf文件

点击查看pdf


function finPdf(){

window.open("/devDoc?storeName=文件名.pdf", "_blank","top=200,left=200,height=600,width=800,status=yes,toolbar=1,menubar=no,location=no,scrollbars=yes");


以上写在页面

 //storeName是文件名

 @RequestMapping("/devDoc")
@ResponseBody
public void devDoc(HttpServletRequest request, HttpServletResponse response, String storeName) throws Exception {
request.setCharacterEncoding("UTF-8");
String ctxPath = request.getSession().getServletContext().getRealPath("");
String downLoadPath = ctxPath + "/file/" + storeName;
response.setContentType("application/pdf");
FileInputStream in = new FileInputStream(new File(downLoadPath));
OutputStream out = response.getOutputStream();
byte[] b = new byte[512];
while ((in.read(b))!=-1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}

你可能感兴趣的:(java)