处理过程:
后台生成PDF
[code]
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try{
PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
document.add(new Paragraph("Hello World"));
}catch(DocumentException de){
de.printStackTrace();
System.err.println("A Document error:" +de.getMessage());
}
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
[/code]
前台获取:
[code]function getPDF()
{
document.location.href="servlet/GetPDF";
}
[/code]
问题是这样操作的话,pdf文件直接在浏览器上显示了,那如何直接提示用户下载到本地那?
后台生成PDF
[code]
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try{
PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
document.add(new Paragraph("Hello World"));
}catch(DocumentException de){
de.printStackTrace();
System.err.println("A Document error:" +de.getMessage());
}
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
[/code]
前台获取:
[code]function getPDF()
{
document.location.href="servlet/GetPDF";
}
[/code]
问题是这样操作的话,pdf文件直接在浏览器上显示了,那如何直接提示用户下载到本地那?