java - 如何使用Java将生成的PDF文件保存到MySQL数据库?

标签 java mysql pdf

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

我担心的是:

  1. 我应该在 PDF 表的 MySQL 列中使用什么数据类型来保存 PDF 文件
  2. 哪个查询会将生成的 PDF 文件插入数据库

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

这是我用 Java 编写的 PDF 生成代码:

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..");


请帮助我。
提前致谢。

最佳答案

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

    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();
    }
    
  3. 将其插入数据库如果您使用任何 ORM 工具,您只需将列映射为 blob,该工具就会为您处理它。如果您不使用它,那么您可以创建一个准备好的语句。 Statement 有一个名为 setBlob() 的方法,该方法很有用。考虑下面的示例并创建一个带有 blob 列的普通插入查询。

    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 - 如何使用Java将生成的PDF文件保存到MySQL数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19970964/

相关文章:

mysql - 如果存在,如何进行 MySQL 查询以获取其他表字段值?

pdf - WkHtmlToX Sharp 如何添加页眉和页脚?

python - 用 Python 阅读 PDF 包?

java - 是否可以添加库项目的 JAR 文件并在另一个项目中使用其资源?

java - 如何表达Java Double Array类型(修复反汇编代码)

java - 如何在java中正确读取阿拉伯数据集?

mysql - 如何在谷歌计算引擎上将node.js连接到mysql?

mysql - 我需要连接三个表,按一列对结果进行分组,并显示另一列的最大值

java - 流式 PDF 在浏览器中以文本形式打开

java - 如何在 JSON 文件的不同数组和对象中导航