我有一个存储在会话中的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>
现在,我必须在控制器中编写另一个具有相同映射(retrievePdf
)的方法,该方法将显示PDF。
@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);
}
就是这样,它将起作用!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句