pdf.js实现加载远程pdf和本地文件pdf(不是在webapp下),完美解决跨域问题

目标文件:远程pdf文件和本地文件(不是放在webapp下的文件,是电脑任意磁盘上的本地文件)

解决思路:

       1.都是以文件流的形式来进行加载pdf文档,完美解决跨域问题

       2.写一个servlet,将文件流输出,浏览器可直接打开,展示该pdf

注意: 至于pdf.js插件的使用,上官网即可,我不做过多解释了,网上也有很多

前端展示:http://localhost:6443/web/PDFDemo/pdfjs/generic/web/viewer.html?file=URL

其中URL=你写的servlet的链接

加载远程文件 java servlet代码:

package main;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@WebServlet(name = "Convert")
public class Convert extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/pdf");
        response.setHeader("Access-Control-Allow-Origin", "*"); // 解决请求头跨域问题
        ServletOutputStream sos = response.getOutputStream();
        //这里的 destUrl目前是固定的,实际应用中需要动态获取
        String destUrl = "http://39.107.117.248/pdf-store/caseFile_origin.pdf"; // 远程文件途径,这是我放在远程服务器的
        //tomcat /webapp下的文件,该链接可通过浏览器直接打开,你可以用它来进行测试;
        URL url = new URL(destUrl);
        HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
        // 连接指定的网络资源
        httpUrl.connect();
        // 获取网络输入流
        BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
        int b;
        while ((b = bis.read())!= -1){
            sos.write(b);
        }
        sos.close(); // 这里有点和c语言里的读取文件有点像
        bis.close();
    }
}

加载本地文件 java servlet 代码:

package main;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet(name = "ConvertLocal")
public class ConvertLocal extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileName = request.getParameter("fileName");
        response.setHeader("Access-Control-Allow-Origin", "*"); // 解决请求头跨域问题
        response.setContentType("application/pdf");
        File f1 = new File("E:\\testfiles-pdf\\"+fileName);
        FileInputStream reader= new FileInputStream(f1);
        BufferedInputStream BufferReader = new BufferedInputStream(reader);
       // 创建servlet 输出流对象
        ServletOutputStream sos = response.getOutputStream();


        int b;
        while ((b = BufferReader.read())!= -1){
            sos.write(b);
        }
        sos.close(); // 这里有点和c语言里的读取文件有点像
        reader.close();
    }
}

 

你可能感兴趣的:(pdf.js,java,servlet,JavaWeb后端开发)