生成的pdf文件保存在项目文件夹内

Generated pdf file save inside the project folder

本文关键字:项目 项目文件 文件夹 存在 保存 pdf 文件      更新时间:2023-09-26

我想将生成的PDF文件保存在我的项目文件夹中,使用此代码。我已经成功地生成了它,想要保存它在我的项目文件夹中,jsp文件存在的地方,下面是我的代码

 <%@ page import="java.servlet.*,
        javax.servlet.http.*,
        java.io.*,
        java.util.*,
        com.itextpdf.text.pdf.*,
        com.itextpdf.text.*,java.sql.*" %><%
response.setContentType("application/pdf"); Document document = new
Document(); try{
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, buffer);
    document.open();

    PdfPTable table = new PdfPTable(2);
    table.addCell("1");
    table.addCell("2");
    table.addCell("3");
    table.addCell("4");
    table.addCell("5");
    table.addCell("6");       

    document.add(table);       
    document.close();
    DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
    byte[] bytes = buffer.toByteArray();
    response.setContentLength(bytes.length);
    for(int i = 0; i < bytes.length; i++)
    {
        dataOutput.writeByte(bytes[i]);
    }
    }catch(DocumentException e){
    e.printStackTrace(); }
  %>

thanks in advance.

使用getServletContext().getRealPath(..)