1

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

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44

2 Answers2

2

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

0

I don't 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 PreparedStatement, 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[].

Bampfer
  • 2,120
  • 16
  • 25
hinneLinks
  • 3,673
  • 26
  • 40
  • Unfortunately I'm not using preparedstatement for this project,, I'm using liferay, kinda similar with struts, correct me if Im wrong haha... it's called Service Builder... it's based on hibernate and spring – John Voltaire Maximo Aug 20 '15 at 05:42
  • the field in the database is of Blob data type, can I insert byte arrays in Blob fields? – John Voltaire Maximo Aug 20 '15 at 05:43
  • I edited my Answer for the Blob-Object. You should be able to insert a byte[] in a Blob Field in DB. – hinneLinks Aug 20 '15 at 05:45
  • I did this, but I get error // Serialize to a byte array ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutput out1 = new ObjectOutputStream(outputStream); out1.writeObject(file); out1.close(); // Get the bytes of the serialized object byte[] fileArray = outputStream.toByteArray(); return Blob.setBytes(0, fileArray); – John Voltaire Maximo Aug 20 '15 at 07:00
  • oh sorry I didn't know codes couldn't be formatted at comments – John Voltaire Maximo Aug 20 '15 at 07:02
  • see here http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java for how to convert a inputstream to byte array. – hinneLinks Aug 20 '15 at 08:36