问题:
pdf.js下载下来之后,接入到应用中之后,可以通过 var DEFAULT_URL = '';
读取同目录下的pdf文件。但是存在其他磁盘文件就不可以了。
解决思路:
Servlet代码如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Kevin 2018-4-24
*
* pdf处理
*
*/
@WebServlet("/PDFServlet")
public class PDFServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PDFServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 此代码仅为示例,具体的防空指针等、业务逻辑需要根据实际情况更改。 2018-4-24
*/
String paramId = request.getParameter("pdfId");
//下面是伪代码,真正的业务需要查库,得到path
String path = "";
if(paramId.equals("1")){
path = "D:\555\2017-09-116 (1).pdf";
} else {
path = "D:\我的开源世界\JDK中的设计模式应用实例-动力节点.pdf";
}
InputStream input = null;
OutputStream out = null;
try{
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/pdf;charset=UTF-8");
input = new BufferedInputStream(new FileInputStream(new File(path)));
byte buffBytes[] = new byte[1024];
out = response.getOutputStream();
int read = 0;
while ((read = input.read(buffBytes)) != -1) {
out.write(buffBytes, 0, read);
}
}catch(Exception e){
e.printStackTrace();
}finally {
out.flush();
out.close();
}
}
}
pdf.js中web/viewer.js中需要将下述代码放到var DEFAULT_URL = '';
将其重新赋值:
/***************** 定义方法获取本地文件,通过重置URL,让PDFServlet处理流 wgd*******************/
var pdfId = document.getElementById('vie').getAttribute('data');
var DEFAULT_URL = "";//注意,删除的变量在这里重新定义
var PDFData = "";
//获取应用路径
var localObj = window.location;
var contextPath = localObj.pathname.split("/")[1];
var basePath = localObj.protocol + "//" + localObj.host + "/"+ contextPath;
$.ajax({
type:"post",
async:false, //
mimeType: 'text/plain; charset=x-user-defined',
url:basePath+'/PDFServlet?pdfId=' + pdfId,
success:function(data){
PDFData = data;
}
});
var rawLength = PDFData.length;
//转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(var i = 0; i < rawLength; i++) {
array[i] = PDFData.charCodeAt(i) & 0xff;
}
DEFAULT_URL = array;
/*****************wgd 2018-4-24*******************/
引入viewer.js的时候需要将参数,比如:一条附件信息的id传入等,利用下面这种方式即可,当然搜索还有其他的方式可以解决。
<script id="vie" src="<%=request.getContextPath() %>/js/pdf-js/web/viewer.js"
data="${param.id }"></script>
通过上面的办法,就可以展示存储的本地pdf文件了。