gpt4 book ai didi

java - 如何在不在服务器端存储文件的情况下向浏览器提供 PDF?

转载 作者:行者123 更新时间:2023-12-03 21:44:50 24 4
gpt4 key购买 nike

我有两种方法。一个在服务器端生成 PDF,另一个在客户端下载 PDF。

如何在不将其存储在服务器端并允许客户端直接下载的情况下执行此操作。

以下是两种方法:

public void downloadPDF(HttpServletRequest request, HttpServletResponse response) throws IOException{

response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment;filename="+ "testPDF.pdf");
FileInputStream fis = null;
DataOutputStream os = null;

try {
File f = new File("C://New folder//itext3.pdf");
response.setHeader("Content-Length",String.valueOf(f.length()));

fis = new FileInputStream(f);
os = new DataOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
fis.close();
os.flush();
os.close();
}
response.setHeader("X-Frame-Options", "SAMEORIGIN");
}

并且:

public Document generatePDF() {

Document doc = new Document();
try {
File file = new File("C://New folder//itext_Test2.pdf");
FileOutputStream pdfFileout = new FileOutputStream(file);
PdfWriter.getInstance(doc, pdfFileout);

doc.addAuthor("TestABC");
doc.addTitle("Aircraft Details");
doc.open();


Anchor anchor = new Anchor("Aircraft Report");
anchor.setName("Aircraft Report");

Chapter catPart = new Chapter(new Paragraph(anchor), 1);

Paragraph para1 = new Paragraph();
Section subCatPart = catPart.addSection(para1);
para1.add("This is paragraph 1");

Paragraph para2 = new Paragraph();
para2.add("This is paragraph 2");


doc.add(catPart);

doc.close();


} catch (Exception e) {
e.printStackTrace();
}
return doc;
}

最佳答案

建议您使用 response.getOutputStream() 而不是创建 FileOutputStream 的人是对的。参见例如 Hello我书第 9 章中的 Servlet:

public class Hello extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
try {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, response.getOutputStream());
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));
// step 5
document.close();
} catch (DocumentException de) {
throw new IOException(de.getMessage());
}
}
}

但是,当您像这样直接发送字节时,某些浏览器会遇到问题。使用 ByteArrayOutputStream 在内存中创建文件并告诉浏览器在内容 header 中它可以期望多少字节会更安全:

public class PdfServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// Get the text that will be added to the PDF
String text = request.getParameter("text");
if (text == null || text.trim().length() == 0) {
text = "You didn't enter any text.";
}
// step 1
Document document = new Document();
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
document.add(new Paragraph(String.format(
"You have submitted the following text using the %s method:",
request.getMethod())));
document.add(new Paragraph(text));
// step 5
document.close();

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
}
catch(DocumentException e) {
throw new IOException(e.getMessage());
}
}
}

有关完整的源代码,请参阅 PdfServlet .您可以在此处尝试代码:http://demo.itextsupport.com/book/

关于java - 如何在不在服务器端存储文件的情况下向浏览器提供 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com