java 将我的文件转换为 blob,然后插入到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32110221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting my files to blob then inserting to database
提问by John Voltaire Maximo
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..
然后这会将文件转换为 blob,我认为..
/* 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...
但是当我运行它时,我得到 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??
我的转换有什么问题,我试图找到从文件到 blob 的直接转换,但对我没有任何作用,也许我需要先转换为其他东西然后 blob?
please help,, thanks guys
请帮忙,谢谢各位
采纳答案by John Voltaire Maximo
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
因为我使用 Liferay 将我的文件插入数据库..我使用了以下代码,似乎 Liferay 创建了一个名为“OutputBlob”的对象来处理 blob 对象..这里是代码
/* 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
在那里,这就是解决我的问题的原因,请记住它仅适用于 Liferay 上传,OutputBlob 对象不是在 Liferay 以外的任何其他平台/语言/框架上制作的...感谢您的帮助,我希望有人在那里Liferay 上传有问题,请参阅:D
回答by hinneLinks
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.
我不认为演员会起作用。Blob 由字节组成,因此您需要将文件内容读入 byte[] 并使用java.sql.Blob.setBytes(long, byte[])
Blob的-Methods。由于它是一个接口,如果您确实需要一个 Blob 对象,类 javax.sql.rowset.serial.SerialBlob 可能会帮助您。
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[].
如果使用 PreparedStatement,则java.sql.PreparedStatement.setBlob(int, InputStream)
无需先创建 byte[]即可使用。如果您使用 JPA/Hibernate afaik,它应该能够使用字节 []。