1.在线查看pdf
前端:主要使用了pdfJs,后端仅需使用
HttpServletResponse获取流输出对象(OutputStream)对前端进行响应即可
//点击查看公司经营备案凭证
$("#supplierBusinessCertificate").click(function (){
window.open("${pageContext.request.contextPath}/js/pdfJs/web/viewer.html?file="
+encodeURIComponent("${pageContext.request.contextPath}/supplierInfo/selSupplierBusinessCertificateById?supplierId="+ supplier.supplierId +""));
})
后端:
@GetMapping("/selSupplierBusinessLicenseById")
public void selSupplierBusinessLicenseById(Long supplierId,HttpServletRequest request, HttpServletResponse response) {
SupplierInfo byId = supplierInfoService.getOne(new QueryWrapper<SupplierInfo>()
.select("SUPPLIER_BUSINESS_LICENSE").eq("SUPPLIER_ID",supplierId));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + byId.getSupplierName()+"的营业执照.pdf");
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
System.out.println("==================pdf处理开始==================");
os = response.getOutputStream();
//获得PDF文件流,核心代码
InputStream is = new ByteArrayInputStream(byId.getSupplierBusinessLicense());
System.out.println("获取流结束。。。。");
bis = new BufferedInputStream(is);
int i = 0;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (Exception e) {
System.out.println("pdf处理出现异常:" + e.getMessage() + "; ");
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.查看文档
同样使用pdfJs即可,基本同上,这种方法适用于我的项目。
$("#wenhao2").click(function (){
layer.msg("请稍等")
window.open("${pageContext.request.contextPath}/js/pdfJs/web/viewer.html?file="
+encodeURIComponent("${pageContext.request.contextPath}/supplierInfo/getSuperviseType"));
})
@GetMapping("/getSuperviseType")
public void getSuperviseType(HttpServletResponse response) throws IOException {
System.out.println("查看医学装备目录doc文档被访问");
// File file = new File("C:\\Users\\Administrator\\Desktop\\医学装备 分类目录.pdf");
File file = new File("/home/xb/tomcat8/webapps/pdf/医学装备 分类目录.pdf");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=医学装备 分类目录.pdf");
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
System.out.println("==================pdf处理开始==================");
os = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
// fileInputStream.read
//获得PDF文件流,核心代码
// InputStream is = new ByteArrayInputStream();
// System.out.println("获取流结束。。。。");
bis = new BufferedInputStream(fileInputStream);
int i = 0;
while ((i = fileInputStream.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (Exception e) {
System.out.println("pdf处理出现异常:" + e.getMessage() + "; ");
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("查看医学装备目录doc文档完成");
}
本文仅用于记录我的代码,若能帮助到您,不胜荣幸