我有这个RPC服务,它在字节数组中创建Document并在HttpSession中写入,然后在客户端onSuccess块中调用一个Servlet,它向客户端发送PDF。 String token = "258958395ai53"
是客户端找到PDF的令牌,但在此示例中,y使令牌成为静态,因此我需要随机创建令牌并确保令牌不会重复。这是代码。
RPC服务:
public String getPdf() {
Document document = new Document();
String token = "258958395ai53";
// generate test PDF
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("¡HOLA PUTO MUNDO!"));
document.close();
byte[] pdf = baos.toByteArray();
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
session.setAttribute(token, pdf);
} catch (Exception e) {
System.out.println("ReportServlet::generatePDF::Exception "
+ e.getMessage());
}
return token;
}
的Servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// create output stream from byte array in session
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String token = request.getParameter("token");
byte[] pdf = (byte[]) request.getSession().getAttribute(token);
baos.write(pdf);
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0,pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
// content length is needed for MSIE
response.setContentLength(baos.size());
// write ByteArrayOutputStream to ServletOutputStream
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
}
的onSuccess:
public void onSuccess(String lista) {
String token = lista;
//Window.open("hello?token="+ token, "_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
Dialog d = new Dialog();
d.setWidth(500);
d.setHeight(700);
d.setUrl(GWT.getModuleBaseURL()+"hello?token="+token);
d.show();
}
});
有什么想法吗? ..可以查看我的示例http://pdfprueba2.appspot.com/