如何使用JSON将PDF写入客户端

时间:2014-05-14 15:28:43

标签: java json servlets pdf base64

我正在尝试使用servlet / JSP和jquery动态地将PDF写入客户端。以下是我到目前为止的情况:

SERVLET

public class IndexServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private static MimetypesFileTypeMap mtp = new MimetypesFileTypeMap();

  public IndexServlet() {
  super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {
  System.out.println("Sysout of doGet from IndexServlet");

    ////INIT OF VARs
    int pdfNum,
        pdf;
    String[] currentBatch;
    String 
             mimeType = null, 
             fileID = null,
             rtn = null,  
             docSelect = null, 
             docNumber = null, 
             docType = null,
             batchName = null,
             event = request.getParameter("EVENT"),
             pendDir = "\\\\****\\****\\****\\****\\****\\****\\****";

    ///////////////////

if(event.equals("LOAD")){

    System.out.println("You're now in the LOAD portion of the doGet");

    PdfInit p = new PdfInit();
    Batch b = p.getBatch();
    currentBatch = b.pdfFiles;
    batchName = b.batchName;
    pdfNum = b.pdfNum;

    Gson gson = new Gson();
    String json = gson.toJson(b);

    System.out.println(json);

    response.setContentType("application/json");
    response.getWriter().write(json);

}else if(event.equals("GETPDF")){

    System.out.println("You're now in the GETPDF portion of the doGet");    


    PdfInit p = new PdfInit();
    Batch b = p.getBatch();
    currentBatch = b.pdfFiles;
    batchName = b.batchName;
    pdfNum = b.pdfNum;

    pdf = Integer.parseInt(request.getParameter("id"));
    String fileName = pendDir + "\\" + batchName + "\\" + currentBatch[pdf];

    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ByteArrayDataSource bais = new ByteArrayDataSource(bis, mimeType);

    byte [] pdfArray = bais.toByteArray();
    String encoded = DatatypeConverter.printBase64Binary(pdfArray);

    Gson gson = new Gson();
    String json = gson.toJson(encoded);

    response.setContentType("application/json");
    response.getWriter().write(json);
    response.getOutputStream().flush();
    response.getOutputStream().close();

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-     8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script type="text/javascript" src="/Indexer/_js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="/Indexer/_js/jquery.form.js"></script>

</head>
<body>

<input type="button" value="Load Batch" id="LoadBatchBtn">

<p id="batchName"><b>Batch Name:</b> </p>
<p>Click on a document to view it.</p>             

<ol id="pdfList"></ol>
<div id="pdfDiv"></div>

</body>
</html>

<script>

$(document).ready(function(){

 $("#LoadBatchBtn").click(function(){
   $.getJSON("IndexServlet", {"EVENT": "LOAD"}, function(data) {
     $("#batchName").append(data.batchName);

        for(var i = 0; i < data.pdfNum; i++ )
        {
          $("#pdfList").append("<li id=" + i + ">" + data.pdfFiles[i] + "</li>");
        }

 $("li").click(function(){
  $("#pdfDiv").hide();
   $.getJSON("IndexServlet", {id: this.id, "EVENT":"GETPDF"}, function(data){
      var decodedData = window.atob(data);
      $("#pdfDiv").html(decodedData).show();
           });
         });        
      });
   });          
}); 

</script>

现在我在JSP上根本没有得到任何东西。但是在控制台中我可以看到传递的base64字符串没有问题。我知道我很可能需要更进一步,但我不知道从哪里开始。

修改

好的,现在我在div中获得字符串输出。如何将其转换为浏览器以PDF格式读取的数据?

0 个答案:

没有答案