1.后端返回文件流,可在浏览器访问显示pdf的请求路径。
直接的话是调用的window的pdf。我想要隐藏下载和打印。做不到。
后端代码:
/**
* 展示文件,主要是先读取,再输出
* @param path
* @param response
*/
@RequestMapping(value = "/showFile")
public void showFile(
@RequestParam(name = "path") String path, HttpServletResponse response,
HttpServletRequest request) {
String pdfPath="C:/Users/lhl/Desktop/files/pdf/b3c39656ea744095bd9eb52d48c51803.pdf";
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
System.out.println(path);
//header中的这个*指所有域名,参数指的是域名。。可以指定允许特定的域名
response.setHeader("Access-Control-Allow-Origin", "*");
File file = new File(pdfPath);
System.out.println("file"+file.exists());
System.out.println("----------");
if (file.exists()) {
System.out.println("文件存在");
byte[] buffer = new byte[2048];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream outputStream = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
bis.close();
outputStream.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else {
System.out.println("文件不存在---------------------------");
}
}
2.下载pdf.js http://mozilla.github.io/pdf.js/
l
其中viewer.html访问会出现默认的pdf.
原路径?file=你的pdf文件。即可达到要求。
关于隐藏上面的按钮,直接在view.html找到相应的按钮,style=display:none即可,也可以在上面写button或者a链接进行返回的操作。
你会遇到错误;
开修改web下的viewer.js, 将DEFAULT_URL的值设为空字符串‘’,这样就能通过传参设置你想看的pdf文件了,
遇到:file origin does not match viewer's错误;
解决方案:webViewerInitialized()中注释验证url的函数://validateFileURL(file);