abden003
abden003

Reputation: 1335

Saving file to database in vaadin

Hello I am trying to save a file into my database using vaadin. I have a web application which allows the user to upload a file, so far the file uploads to my filesystem. Here is my code for the upload:

public class FileUploader implements Receiver, SucceededListener {
        File file;

        public OutputStream receiveUpload(String fileName, String mimeType) {
            FileOutputStream fos = null;

            try {
                file = new File("C:\\Documents and Settings\\ABDEN00U\\Desktop\\tmp\\" + fileName);
                fos = new FileOutputStream(file);   
            } catch (final java.io.FileNotFoundException e) {
                new Notification("Could not open file", e.getMessage(), Notification.TYPE_ERROR_MESSAGE.ERROR_MESSAGE).show(Page.getCurrent());
                return null;
            }

            return fos; 
        }

        @Override
        public void uploadSucceeded(SucceededEvent event) {
            // TODO Auto-generated method stub
            file_upload.setVisible(true);
            file_upload.setSource(new FileResource(file));
        }

What I am trying to do is grab the file and convert it to a bytearray and upload it to my postgres database.

Upvotes: 1

Views: 3079

Answers (1)

nexus
nexus

Reputation: 2937

In your uploadSucceeded method you can convert your file into a byte[].

How? Here's a possible solution.

Then store your byte[] into a blob field in your database.

Upvotes: 2

Related Questions