1.官网下载PDF.js
我的资源地址:https://download.csdn.net/download/rexueqingchun/11974119
2.下载解压后放到项目资源文件目录中
如上图所示,Springboot项目可直接放在static目录下
3.预览PDF文件
由于PDF插件的模版文件为viewer.html,所以预览请求的固定地址为http://localhost/pdfjs/web/viewer.html?file=参数
这里有两种不同的参数格式,分别为预览项目内部文件和外部文件:
(1)预览项目内部文件
如:项目static下面的upload/劝阻工作指引.pdf
预览地址为http://localhost/pdfjs/web/viewer.html?file=/upload/劝阻工作指引.pdf
(2)预览项目外部文件
如:预览磁盘上的文件C:\FTP\upload\劝阻工作指引.pdf,则发送请求到后台处理
预览地址为http://localhost/pdfjs/web/viewer.html?file=/ggzyYl
请求的后台代码:
@RequestMapping(value = "ggzyYl", method = {RequestMethod.GET, RequestMethod.POST })
public void ggzyYl(HttpServletRequest request, HttpServletResponse response) {
String path = "C:/FTP/upload/劝阻工作指引.pdf";
File file = new File(path);
byte[] data = null;
try {
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e){
System.out.println("文件处理异常");
}
}