日期:2014-05-16  浏览次数:20394 次

java 大文件以二进制保存到数据库
一、创建表
oracle:

create table baoxiandan (
  id number(20) not null,
  fileName varchar2(200) not null,
  content blob,
  primary key(id)
  );
create sequence seq_baoxiandan;

二、Hibernate映射文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.erry.tntops.acer.model.impl.CarrierImpl"
           table="BAOXIANDAN"
           proxy="com.erry.tntops.acer.model.Carrier">
        <id name="id" column="ID" type="long" unsaved-value="-1">
            <generator class="sequence">
                <param name="sequence">SEQ_BAOXIANDAN</param>
            </generator>
        </id>
        <property name="fileName" column="filename" type="java.lang.String" not-null="false">
            <meta attribute="caption">${tntops.acer.Carrier.code}</meta>
        </property>
        <property name="content" column="content" type="byte[]">
            <meta attribute="caption">${tntops.acer.Carrier.name}</meta>
        </property>
    </class>
</hibernate-mapping>

三、pojo类


import com.erry.tntops.common.model.impl.BaseModelImpl;
import com.erry.tntops.acer.model.Carrier;

import java.util.Date;

/**
* Created by IntelliJ IDEA.
* User: chenyang
* Date: 2008-7-28
* Time: 17:46:32
* To change this template use File | Settings | File Templates.
*/
public class CarrierImpl extends BaseModelImpl implements Carrier {

    private long id;
    private String fileName;
    private byte content[];

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }
}

四、Java代码
1、入库
            //获得文件名,fileNamePath为文件路径变量
            String fileName = fileNamePath.substring(fileNamePath.lastIndexOf("\\") + 1);
   &nbs