converting my files to blob then inserting to database

I have a problem with inserting my uploaded files via action class..

I am using this code on the action class

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
/*  Upload NBI  */
File nbi = null;
File nbiFile = getThisFile(uploadRequest, "nbiFile", nbi);

it's calling this method to get the file

public File getThisFile(UploadPortletRequest uploadRequest, String filename, File file){
InputStream[] inputStream;
try {
    inputStream = uploadRequest.getFilesAsStream(filename);
    for(InputStream fileObj:inputStream){
        file = FileUtil.createTempFile(fileObj);
    }
} catch (IOException e) {
    e.printStackTrace();
}
return file;
}

then this converts the file to blob,, I think..

/*  Convert files to blob  */
Blob blobNbi = Blob.class.cast(nbiFile);

Then I should be able to save it

/*  Attachment Object Properties  */
long attachment_id = CounterLocalServiceUtil.increment();
attachments a = null;
a = attachmentsLocalServiceUtil.createattachments((int) attachment_id);
a.setNbi_clearance(blobNbi);

/*  Save the data to table  */
a = attachmentsLocalServiceUtil.addattachments(a);

but when I run it I get ClassCastException: cannot cast java.io.File to java.sql.Blob...

What is wrong with my conversion, I'm trying to find direct casting from file to blob but nothing works for me,, perhaps I need to convert to something else first then blob??

please help,, thanks guys

Answers


I dont think a Cast would work. A Blob consists of bytes, so you need to read your File Contents into a byte[] and use the java.sql.Blob.setBytes(long, byte[])-Methods of Blob. Since its an Interface, the Class javax.sql.rowset.serial.SerialBlob might help you, if you really need a Blob-Object.

If you use a PrepearedStametemnt, you can use java.sql.PreparedStatement.setBlob(int, InputStream) without making a byte[] first. If you use JPA/Hibernate afaik it should be able to work with a byte[].


Because I'm using Liferay to insert my files into the database.. I used the following code,, it appears that Liferay has created an object called 'OutputBlob' to handle blob objects.. here's the code

/*  Get file from jsp  */
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
File file = uploadRequest.getFile("XXX", true); // XXX is the input name from my jsp
FileInputStream fileInput = new FileInputSream(file);
OutputBlob blobOutput = new OutputBlob(fileInput, file.length());

/*  Attachment Object Properties  */
long attachment_id = CounterLocalServiceUtil.increment();
attachments a = null;
a = attachmentsLocalServiceUtil.createattachments((int) attachment_id);
a.setNbi_clearance(blobOutput);

/*  Save the data to table  */
a = attachmentsLocalServiceUtil.addattachments(a);

There, that's what solved my problem,, remember it only works for liferay uploading,, OutputBlob object isn't made on any other platform/language/framework other than Liferay... thanks for the help guys, I hope someone out there who has problems with liferay uploading see this :D


Need Your Help

How to make a photo gallery like application with jquery mobile using highcharts instead of images?

jquery html5 mobile highcharts

Hi guys Im working on a mobile web site using jquery mobile, saw many examples of photo gallery integration with jquery mobile tried most of them and they work out just fine. My problem is instead...

iPhone : Getting a value back from views

iphone objective-c

From the view A I open the View B which contains categories in a uitableview, when I select a category I open the view C which contains subcategories for the category (using the CoreData persistenc...