0

我正在从 HTML 上传文件

 <center><form action="pdf" method="post" enctype="multipart/form-data">
       <b>Upload Certificate</b>
       <input type="file" name="file"/></center>
      <center> <input type="submit" /></center>
</form>

在提交表单时,会调用 pdf Servlet。在 servlet 内部,请求对象被解析并使用 InputStream 读取文件(pdf),如下面的代码所示。

protected void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    try
    {
      List localList = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(paramHttpServletRequest);
      for (FileItem localFileItem : localList)
      {
        String str1 = localFileItem.getFieldName();
        String str2 = FilenameUtils.getName(localFileItem.getName());
        System.out.println("fieldname:" + str1);
        System.out.println("filename:" + str2);
        InputStream localInputStream = localFileItem.getInputStream();
        try
        {
          PdfReader localPdfReader = new PdfReader(localInputStream);
          paramHttpServletResponse.sendRedirect("takedetails.jsp");
        }
        catch (InvalidPdfException localInvalidPdfException)
        {
          paramHttpServletResponse.sendRedirect("upload.jsp");
        }

      }

    }
    catch (FileUploadException localFileUploadException)
    {
      throw new ServletException("Cannot parse multipart request.", localFileUploadException);
    }
  }

如您所见,我使用 InputStream 对象来检查文件格式是否为 pdf。

现在我想将此 pdf 文件保存到 postgresql 数据库。我应该在 postgresql 中使用什么字段,如何从 InputStream 对象中获取文件以将其存储在数据库中?

4

2 回答 2

2

目前尚不清楚您使用的是什么持久性 API。JDBC?JPA?很好的冬眠?我将假设 JDBC。在 JDBC 中,您可以使用PreparedStatement#setBinaryStream()将 a 存储InputStream在数据库中,或者PreparedStatement#setBytes()将 a 存储byte[]在数据库中。无论哪种方式,在 PostgreSQL 中,您都需要一个bytea列。

当您首先验证上传的文件时PdfReader,这InputStream是不合适的。即只能读取一次。每次您需要再次阅读时,客户端不会多次重新发送文件InputStream。你需要先复制InputStream到一个byte[]

ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(localFileItem.getInputStream(), output);
byte[] filecontent = output.toByteArray();

IOUtils是 Apache Commons IO 的一部分;如果您使用 FileUpload,那么您已经拥有它)

不要忘记更改 iText 以使用byte[]

PdfReader localPdfReader = new PdfReader(filecontent);

通过 iText 对其进行验证后,您可以bytea使用 JDBC 将其存储在 PostgreSQL 列中,如下所示:

statement = connection.prepareStatement("INSERT INTO files (name, content) VALUES (?, ?)");
statement.setString(1, filename);
statement.setBytes(2, filecontent);
statement.executeUpdate();
于 2012-04-25T21:12:26.933 回答
1

这个论坛有一个类似的问题。它使用图像而不是pdf。但程序可能相同。将流保存到文件并将其存储到数据库中。看看这个。也许可以帮助你。

For example, suppose you have a table containing the file name of an image and you also want to store the image in a bytea column:

CREATE TABLE images (imgname text, img bytea);

To insert an image, you would use:

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
ps.executeUpdate();
ps.close();
fis.close();
于 2012-04-25T17:36:21.690 回答