首页 > 解决方案 > 将图像和 PDF 从电子邮件附件加载到 BLOB

问题描述

我正在尝试学习如何将附件从我的 gmail 保存到 Oracle Database BLOB 服务器端。

这是我的代码的一部分:

static void InsertAttachment(String i_in, Message message) 
throws SQLException, IOException, MessagingException  
{
Object content = message.getContent();
if (content instanceof Multipart)
    {
    Multipart multipart = (Multipart)message.getContent();
    for (int i=0, n=multipart.getCount(); i<n; i++) 
        {
        Part part = multipart.getBodyPart(i);
        String disposition = part.getDisposition();
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) 
           {
            String fileName = i_in+"_"+part.getFileName().replace(' ','_');
            String contentType = part.getContentType();
            String mimeType = contentType.substring(0,contentType.indexOf(";"));
            InputStream inStream = part.getInputStream();
            Connection conn = DriverManager.getConnection("jdbc:default:connection:");
            PreparedStatement pstmt = conn.prepareStatement("update email_table set blob = ? where id = ?");     
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] tempBuffer = new byte[1]; // 1 B
            int numRead;
            while ((numRead = inStream.read(tempBuffer)) != -1) {
                                outStream.write(tempBuffer);
                                 }
            byte[] attachment = outStream.toByteArray();
            pstmt.setBinaryStream(1, new ByteArrayInputStream(attachment),attachment.length); 
            pstmt.setString(2, i_in);
            pstmt.executeUpdate();
            conn.commit();
            pstmt.close();  
           }
        }
    }
}

它编译和工作没有任何错误。此外,所有文本文件的加载都没有任何问题。我可以阅读并保存它们,但所有图像或 pdf 附件都被此代码保存为空。

我通过 loadjava 实用程序上传 Java Source,并创建 pl/sql 函数以在 Oracle 数据库上运行此代码。我目前使用 Oracle 数据库 12.2。

我是这个话题的新手,你能帮我理解问题是什么吗?

更新:

使用 MT0 的建议,我尝试下载避免数据库的附件。

我使用了这段代码:

static void InsertAttachment(String i_in, Message message) 
throws SQLException, IOException, MessagingException  
{
Object content = message.getContent();
if (content instanceof Multipart)
    {
    Multipart multipart = (Multipart)message.getContent();
    for (int i=0, n=multipart.getCount(); i<n; i++) 
        {
        Part part = multipart.getBodyPart(i);
        String disposition = part.getDisposition();
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) 
           {
            String fileName = i_in+"_"+part.getFileName().replace(' ','_');
            String contentType = part.getContentType();
            String mimeType = contentType.substring(0,contentType.indexOf(";"));
            //new code starts here
            String destFilePath = "D:/Attachment/" + part.getFileName();
              FileOutputStream output = new FileOutputStream(destFilePath);
              InputStream input = part.getInputStream();
              byte[] buffer = new byte[1];
              int byteRead;
              while ((byteRead = input.read(buffer)) != -1) {
                  output.write(buffer, 0, byteRead);
              }
              output.close();
           }
        }
    }
}

此代码已成功保存所有文件。

因此,问题与此代码在服务器上的行为有关。

标签: javaoracleplsql

解决方案


推荐阅读