【问题标题】:Generated pdf file save inside the project folder生成的pdf文件保存在项目文件夹中
【发布时间】:2023-03-29 01:20:02
【问题描述】:

我想将生成的 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(); }

  %>

提前谢谢..

【问题讨论】:

  • 你忘记添加问题了。
  • 另外,如果我们知道为什么会有所帮助?
  • 什么是 Qs?如果您不提供有意义的问题,人们将不会回复。
  • 问题:我想将生成的 pdf 文件保存在当前目录中。我的 jsp 文件所在的位置。

标签: java javascript jsp jsp-tags jspinclude


【解决方案1】:

使用 getServletContext().getRealPath(..)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2022-06-29
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多