如何使用JavaServlet打包pdf文件并获取标准对话框?
如何将pdf转换为zip并获取标准对话框,该对话框会询问用户希望在浏览器中显示pdf还是以zip格式保存到文件系统如何使用JavaServlet打包pdf文件并获取标准对话框?,java,pdf,servlets,itext,Java,Pdf,Servlets,Itext,如何将pdf转换为zip并获取标准对话框,该对话框会询问用户希望在浏览器中显示pdf还是以zip格式保存到文件系统 public class PdfServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
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());
}
}
}
再一次,我得让你去看医生。由于某些原因,您似乎无法访问该站点上可用的资源 请看一个例子: 现在,您可以将存储在
baos
中的字节发送到您的响应对象,如我对上一个问题的回答中所述:
这回答了你问题的第一部分“如何将pdf转换为zip”,我希望这次你能接受我的回答。我已经为你回答了一个问题,虽然你说我的答案对你有用,但你没有接受我的答案。(如果得到答案的人不接受正确答案,为什么会有人想提供好答案?)
至于问题“获取标准”对话框的第二部分,该对话框询问用户希望在浏览器中显示PDF还是以zip格式保存到文件系统,该部分无法回答。没有标准的盒子可以做到这一点。服务器收到请求并发送响应。您只能发送一个响应,因此无法同时发送PDF以内联显示,也无法发送ZIP以作为附件打开,以便用户可以通过对话框进行选择
在将请求发送到服务器之前,您需要为用户提供选择内联PDF、作为atachment的PDF或ZIP文件中的PDF的选项这没有什么标准您确实需要了解StackOverflow的工作原理。你在StackOverflow上的行为不会让人们想回答你的问题。见我对你上一个问题的评论。
// creating a zip file with different PDF documents
ZipOutputStream zip =
new ZipOutputStream(new FileOutputStream(RESULT));
for (int i = 1; i <= 3; i++) {
ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
zip.putNextEntry(entry);
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello " + i));
// step 5
document.close();
zip.closeEntry();
}
zip.close();
// Creating a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// creating a zip outputstream stream
ZipOutputStream zip = new ZipOutputStream(baos);
// prepare entry
ZipEntry entry = new ZipEntry("my_document.pdf");
zip.putNextEntry(entry);
// create PDF
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
document.open();
document.add(new Paragraph("Hello " + i));
document.close();
zip.closeEntry();
zip.close();
OutputStream os = response.getOutputStream();
baos.writeTo(os);