//将文件写入字节数组,可以将BOLB对应的实体类映射为字节数组类型,如下:
映射文件:
<hibernate-mapping>
…………
<property name="fileContent" type="binary">//设置对应的映射type为binary 就可以存储字节数组
<column name="file_content"/>
</property>
…………
</hibernate-mapping>
对应的实体类:
public class FileBean {
…………
private byte[] fileContent; //直接映射为byte[]
…………
}
struts.xml
<action name="uploadFile" class="uploadAction" method="uploadFile">
<interceptor-ref name="fileUploadStack"/>
<interceptor-ref name="defaultStack"/>
<result type="json">
<param name ="includeProperties">success,message</param>
<param name="contentType">text/html</param>
</result>
</action>
JAVA部分代码:
Java.io.File file = fileList.get(x);//获取前台传入的文件
byte[] fileByteArray = new byte[(int)file.length()] //根据文件长度创建字节数组
FileInputStream in = new FileInputStream(file);//获取流
in.read(fileByteArray,0,fileByteArray.length);//将文件流写入字节数组
//最后将字节数组通过Hibernate设置入库就OK 了
FileBean bean = new FileBean();
……
bean.setFileContent();