如何使用Java将生成的PDF文件保存到MySQL数据库? [英] How to save generated PDF files to MySQL database using Java?

查看:378
本文介绍了如何使用Java将生成的PDF文件保存到MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类,该类使用 iText 库生成PDF文件.现在,根据我的需要,我必须将此生成的PDF文件保存到MySQL数据库表中,但是我不知道该怎么做.

I have a Java class which is generating PDF file using iText library. Now as per my need I have to save this generated PDF file to MySQL database table but I have no idea how to do it.

我担心的是:

    我应该在PDF表的MySQL列中使用什么
  1. 数据类型来保存PDF 文件
  2. 哪个查询会将生成的PDF文件插入数据库
  1. what datatype should I use in MySQL column of PDF table to save PDF file
  2. which query will insert generated PDF file to database

目前,我正在生成PDF文件并将其存储到本地磁盘的硬编码文件路径中.

At present I am generating PDF file and storing it into hard-coded file path of my local disk.

这是我的Java PDF生成代码:

Here is my PDF generation code in Java:

OutputStream file = new FileOutputStream(new File("D://timer.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);

//Inserting Table in PDF
PdfPTable table = new PdfPTable(3);

PdfPCell cell = new PdfPCell(new Paragraph("Java4s.com"));

cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(10.0f);
cell.setBackgroundColor(new BaseColor(140, 221, 8));

table.addCell(cell);

table.addCell("Name");
table.addCell("Address");
table.addCell("Country");
table.addCell("Java4s");
table.addCell("NC");
table.addCell("United States");
table.setSpacingBefore(30.0f);  // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(30.0f);   // Space After table starts, like margin-Bottom in CSS

//Inserting List in PDF
List list = new List(true, 30);
list.add(new ListItem("Java4s"));
list.add(new ListItem("Php4s"));
list.add(new ListItem("Some Thing..."));

//Text formating in PDF
Chunk chunk = new Chunk("Welecome To Java4s Programming Blog...");
chunk.setUnderline(+1f, -2f);//1st co-ordinate is for line width,2nd is space between
Chunk chunk1 = new Chunk("Php4s.com");
chunk1.setUnderline(+4f, -8f);
chunk1.setBackground(new BaseColor(17, 46, 193));

//Now Insert Every Thing Into PDF Document
document.open();//PDF document opened........                  
document.add(Chunk.NEWLINE);   //Something like in HTML :-)
document.add(new Paragraph("Dear Java4s.com"));
document.add(new Paragraph("Document Generated On - " + newDate().toString()));
document.add(table);
document.add(list);            //In the new page we are going to add list
document.close();

file.close();

System.out.println("Pdf created successfully..");


请帮帮我.
预先感谢.


Please help me.
Thanks in advance.

推荐答案

  1. 您可以使用的数据类型是 BLOB
  2. 转换PDF文件并将byte[]数组保留在数据库中.

  1. Datatype that you can use is BLOB.
  2. Convert the PDF file and persist the byte[] array in database.

private byte[] getByteArrayFromFile(final Document handledDocument) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InputStream in = new FileInputStream(handledDocument);
    final byte[] buffer = new byte[500];

    int read = -1;
    while ((read = in.read(buffer)) > 0) {
        baos.write(buffer, 0, read);
    }
    in.close();

    return baos.toByteArray();
}

  • 将其插入数据库中如果您使用任何ORM工具,则只需将该列映射为blob,该工具即可为您处理.如果您不使用它,则可以创建一个准备好的语句.语句具有一个称为setBlob()的方法,该方法将非常有用.考虑下面的示例,并使用blob列创建一个普通的插入查询.

  • To insert it into DB If you are using any ORM tools you just have to map the column as blob and the tool will handle it for you. In case you are not using it then you can create a prepared statement. Statement has a method called setBlob() which will be useful. Consider the below example and create a normal insert query with blob column.

    String sql = "INSERT INTO testtable(stringcolumn, blobcolumn) VALUES(?,?)";
    
    PreparedStatement statement = conn.getConnection().prepareStatement(sql);
    statement.setLong(1, version);
    ByteArrayInputStream bais = new ByteArrayInputStream(getByteArrayFromFile(document));
    statement.setBlob(2, bais);          
    statement.execute();
    
    conn.commit();
    

  • 这篇关于如何使用Java将生成的PDF文件保存到MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆