Save file upload in a data base mysql in a field Blob

1
0
-1

Please I need you help,
How can I save some upload files in a data base mysql in a field Blob?. On my process I need to save a lot of documents and download from an other process, for security this files need to be saved in t a DB Mysql, so I want to save my file in to a field with type blob in my database with a groovy script, but i dont know how to get and convert the content of my file in a blob format .

sorry for my bad english

1 answer

1
0
-1

The English is fine, don't worry...

This is for PostgreSQL and will need converting to your requirements, there are some parameters and fields we use that you won't so be careful.

regards
Seán

PS: If this reply answers your question, please mark as resolved.

                        Class.forName("org.postgresql.Driver");
                        String url = "jdbc:postgresql://"+dbParms.get("dbHost")+":"+dbParms.get("dbPort")+"/"+dbParms.get("dbDatabase");
                        if(debug){logger.severe(module+": url: "+url);}
                        con = DriverManager.getConnection(url, dbParms.get("dbUser"),dbParms.get("dbPassword"));
                        if(debug){logger.severe(module+": con: "+con);}

                        PreparedStatement ps = con.prepareStatement("INSERT INTO " + tble
                                        + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

                        // document stuff
                         
                        ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();

                        Document d1 = apiAccessor.getProcessAPI().getDocument(docID);
                        String documentname = d1.getContentFileName();
                        String mimeType = d1.getContentMimeType();
                        // use fileName if title is empty
                        title = (title==null||title.isEmpty()) ? d1.getContentFileName():title;
                        final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(d1.getContentStorageId());
                        final ByteArrayInputStream document = new ByteArrayInputStream(contentByte);
                        int doclength = contentByte.length;

                        if(debug){logger.severe(module+"@String documentname "+documentname);}
                        if(debug){logger.severe(module+"@String mimeType "+mimeType);}
                        if(debug){logger.severe(module+"@title "+title);}
                                       
                        Calendar calendar = Calendar.getInstance();
                        Timestamp date = new java.sql.Timestamp(calendar.getTime().getTime());
                       
                        // Database fields
                        //1 documentid
                        //2 cobitdocumentname
                        //3 documentname
                        //4 versionmajor
                        //5 versionminor
                        //6 versionmini
                        //7 uploader
                        //8 approver
                        //9 uploaddate
                        //10 approvaldate
                        //11 status
                        //12 document
                       
                    ps.setLong(1, 0);
                        ps.setString(2, removeExtension(documentname.replace("_", " "))); // remove
                        ps.setString(3, documentname);
                        ps.setLong(4, 0);
                        ps.setLong(5, 0);
                        ps.setLong(6, 0);
                        ps.setString(7, "Uploader");
                        ps.setString(8, "Approver");
                        ps.setTimestamp(9, date);
                        ps.setTimestamp(10, date);
                        ps.setString(11, "Pending");
                        ps.setBinaryStream(12, document, doclength);
                        ps.executeUpdate();

                        ps.close();
                        con.close();
                       
                } catch (ClassNotFoundException e) {
                        logger.info("After Load Class: ClassNotFoundException");
                } catch (SQLException eSQL) {
                        // TODO Auto-generated catch block
                        eSQL.printStackTrace();
                } catch (DocumentNotFoundException eDoc) {
                        // TODO Auto-generated catch block
                        eDoc.printStackTrace();
                }

Comments

Submitted by vitiellom on Mon, 10/09/2017 - 16:39

Hi, old question but I have a similar problem.
I use the upload widget in a UI form and I would like to save it on an external (mysql) server. To this end, I use a connector out where I plan to get the file uploaded through the UI form. Specifically, I have a variable of type FileInputValue for the file. I am able to fetch the name of the uploaded file and its mimeType through and I save it to a mysql table through the connector.
In order to then display such a saved document (e.g. an image) in a UI form what shall I return from the connector that fetches the data from the mysql db?

Thanks in advance.
Best

Notifications