Search code examples
javadatabaseemail-attachments

How to save e-mail Attachment directly to Database without saving to HDD First?


I found a nice Java script that connects to my email server and gets the new emails content from it. This java program, download the email attachments to my HDD too. But i need to save the attachments (PDF, EXCEL, WORD, IMAGES, etc.) directly to my Database, instead of first save to HDD and then, uploading to my database (i am using Oracle 12C Database) table.

i am Java rookie programmer, any tips to my question is welcome. thanks!

Here is the snip code that save the attachments to HDD:

public void procesMultiPart(Multipart content) {

    try {

        for (int i = 0; i < content.getCount(); i++) {
            BodyPart bodyPart = content.getBodyPart(i);
            Object o;

            o = bodyPart.getContent();
            if (o instanceof String) {
                System.out.println("procesMultiPart");

            } else if (null != bodyPart.getDisposition() && bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) {

                String fileName = bodyPart.getFileName();

                System.out.println("fileName = " + fileName);
                InputStream inStream = bodyPart.getInputStream();
                FileOutputStream outStream = new FileOutputStream(new File(downloadDirectory + fileName));
                byte[] tempBuffer = new byte[4096]; // 4 KB
                int numRead;
                while ((numRead = inStream.read(tempBuffer)) != -1) {
                    outStream.write(tempBuffer);
                }
                inStream.close();
                outStream.close();

            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

Solution

  • Caveat: I can't really test this but this is basically what you're looking for:

        //----------snip
                    InputStream inStream = bodyPart.getInputStream();
                    //The outstream can be any output stream, I switch this to one that writes to memory (byte[]).      
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                    byte[] tempBuffer = new byte[4096]; // 4 KB
                    int numRead;
                    while ((numRead = inStream.read(tempBuffer)) != -1) {
                        outStream.write(tempBuffer);
                    }
    
                    //Handle object here
                    byte[] attachment = outStream.toByteArray(); 
    
                    //Pseudo Code Begins
                    SQL.createAttachment(attachment); //I'm assuming there's a static method to do this
                    inStream.close();
                    outStream.close();
    //-----------------snip
    

    The code is literally the same, you just need to target the data correctly. That means having a connection to your database, writing some SQL (or using a framework) to insert into it etc...

    This is probably outside the scope of a single question answer. How would I handle it? Probably something like this (I'm assuming you can open a connection and have that all working. I obviously don't have a schema).

    static Connection oracle; //Psuedo Code
    //SQL class
    public static createAttachment(byte[] blob)
    { 
      //exception handling skipped 
      Query q = oracle.createQuery("INSERT INTO Attachments Values(?)");
      q.setParameter(0, blob);
      q.execute();  
    }
    

    I hope that points you in the right direction. It isn't comprehensive but it is a solution. It is also a bad design, but it probably isn't an issue for what you're working with. I'm not even addressing resource management in this.