繁体   English   中英

如何在浏览器上显示pdf?

[英]How to display the pdf on a browser?

我有一个存储在会话中的pdf文件字节流。

@RequestMapping(value = "/start.htm")
    public String start(HttpServletRequest request, Model model)
            throws Exception 
{

// do something
request.getSession().setAttribute(uniqueId,bytesOfaPDF);

return "jspname";
}

现在,我已将其放在我的JSP中。

<object id="COB" data="/retrievePdf.htm" type="application/pdf" width="100%" height="100%">

</object>

现在,我必须在我的控制器中编写另一个方法,该方法具有将显示PDF的相同映射( retrievePdf )。

@RequestMapping(value="/retrievePdf.htm", method=RequestMethod.GET)
    public void retrievePdf(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)throws OSOSystemException
    {
        byte[] db = (byte[]) request.getSession().getAttribute(uniqueId);

        response.getOutputStream().write(db);

        response.setContentType("application/pdf");
        response.setContentLength(db.length);


    }

但是我无法理解如何将uniqueId传递给restorePdf?

我该如何实现? 提前致谢。

将唯一ID作为查询字符串传递到URL中

在请求中存储唯一ID

@RequestMapping(value = "/start.htm")
    public String start(HttpServletRequest request, Model model)
            throws Exception 
{

// do something
// storing uniqueId
request.getSession().setAttribute("uniqueId",uniqueId) ;
request.getSession().setAttribute(uniqueId,bytesOfaPDF);

return "jspname";
}

在Jsp中动态地添加URL:

<% String uniqueId=request.getSession().getAttribute("uniqueId") ;
   String url = "/retrievePdf.htm?uniqueId="+uniqueId ;
%>

<object id="COB" data="<%=url%>" type="application/pdf" width="100%" height="100%">

</object>

在以下方法中为uniqueId添加requestParam并对其进行处理

@RequestMapping(value="/retrievePdf.htm", method=RequestMethod.GET)
    public void retrievePdf(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="uniqueId",required=true)String uniqueId,ModelMap modelMap)throws OSOSystemException
    {
        byte[] db = (byte[]) request.getSession().getAttribute(uniqueId);

        response.getOutputStream().write(db);

        response.setContentType("application/pdf");
        response.setContentLength(db.length);


    }

就是这样,它将起作用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM