file保存成blob

来源:互联网 发布:文件数据库 编辑:程序博客网 时间:2024/06/01 13:19

1 model

private Blob xlsfile;

public Blob getXlsfile() {
  return xlsfile;
 }

 public void setXlsfile(Blob xlsfile) {
  this.xlsfile = xlsfile;
 }

 

2 映射

<property name="xlsfile" column="XLSFILE" type="java.sql.Blob"/>

 

3.file转byte[]

public static byte[] File2byte(File file)
 {
  byte[] buffer = null;
  try
  {
   
   FileInputStream fis = new FileInputStream(file);
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   byte[] b = new byte[1024];
   int n;
   while ((n = fis.read(b)) != -1)
   {
    bos.write(b, 0, n);
   }
   fis.close();
   bos.close();
   buffer = bos.toByteArray();
  }
  catch (FileNotFoundException e)
  {
   e.printStackTrace();
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  return buffer;
 }

4.持久化

impmodel.setXlsfile(Hibernate.createBlob(File2byte(xlsfile)));

 

原创粉丝点击