本文所有的代码均在东方通TongWeb服务器,ARM架构信创360浏览器上通过,并给360找了个BUG,哈哈!
注意:pdf.js在TongWeb东方通中并不支持自己转地址,所以pdf.js在东方通中并行不通。
废话不多说 直接上代码
项目包结构:
在 properties或者yml中配置thymeleaf的cache为false即可
后台Controller,如果数据库中没有代码可直接上绝对路径测试,有的话自己把File地址换成获取到的地址即可
@Controller
public class IndexController {
@RequestMapping("/")
public String index() {
return "index";
}
/**
* 读取pdf书籍
* @param response
*/
@RequestMapping(value = "/preview", method = RequestMethod.GET)
public void pdfStreamHandler(HttpServletResponse response) {
//PDF文件地址
File file = new File("/usr/local/javaTest.pdf");
if (file.exists()) {
byte[] data = null;
FileInputStream input=null;
try {
input= new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.setContentType("application/pdf");
response.getOutputStream().write(data);
} catch (Exception e) {
throw new RuntimeException("pdf文件处理异常"+e);
}finally{
try {
if(input!=null){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
前台页面
window.open("/preview"); 这个地方需要在/之前加上自己的打包项目名称,也可以在浏览器地址中自己修改
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8"/>
<title>在线阅读PDF文件</title>
</head>
<script>
function onLineReadPDF() {
window.open("/preview");
}
</script>
<body>
<h1 onclick="onLineReadPDF()">在线阅读PDF文件</h1>
</body>
</html>