我在jsp中编码。我用itext生成了pdf。并尝试将文档保存到数据库(mysql)。
String filename="patient.pdf";
response.setContentType("application/pdf");
Document document = new Document();
PdfWriter pd= PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.addTitle("Dentistree");
document.add(new Paragraph("Hello,"+"\n\t"+"Thanks for making an appointment to Dentistree"+"\n\t"+"You have provided case history for your appointment. Here are the details of the appointment\n\t"+"List of the diseases that you are suffering through are as follows:"+di+"\n\n\nAre you pregnant?\t"+pregnant+"\n\n\nAre nursing a child?\t"+nursing+"\n\n\nDo you chew pan-masala?\t"+pan+"\n\n\nDo you smoke?\t"+smoke+"\n\n\nAllergic?\t"+all+"List of the medicines:"+medicines));
document.close();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dentistree","root", "tiger");
String str="insert into appointment(email,pdf) values(?,?)";
PreparedStatement st = con.prepareStatement(str);
st.setString(1, mail);
st.setBlob(2,pd);
java编码新手。请帮忙
答案 0 :(得分:2)
你的问题充满了矛盾。
response.setContentType("application/pdf");
,这意味着您要将PDF发送到浏览器。如我的评论所示,您应该在内存中创建PDF:
new FileOutputStream(filename)
现在您拥有ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter pd= PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("Hello");
document.close();
对象,您可以从中获取baos
的完整PDF:
byte[]
之前已经在StackOverflow上回答过:Save itext pdf as blob without physical existence.
获得byte[] pdf = baos.toByteArray();
后,如果这是额外要求,您还可以将PDF发送到浏览器。请参阅我对Pdf file not loading properly created by the servlet的回答,了解它是如何完成的。