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" [b]type="byte[]"[/b]>
<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;
[b]private byte content[];[/b]

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);
File file = new File(fileNamePath);
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[] {};
data = [b]inputStreamToByte[/b](inputStream);//将文件保存到字节数组中
Carrier carrier = (Carrier) SpringContext.getBeanOfType(Carrier.class);
carrier.setFileName(fileName);
carrier.setContent(data);
dao.create(carrier);

//将文件保存到字节数组中
private byte [] [b]inputStreamToByte[/b](InputStream is) throws IOException {
ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
int ch;
while((ch = is.read() ) != -1){
bAOutputStream.write(ch);
}
byte data [] =bAOutputStream.toByteArray();
bAOutputStream.close();
return data;
}

2、出库
byte data [] = new byte[]{};
File file =null ;
FileOutputStream fos = null;
InputStream in = null;

String hql = "select carrier from com.erry.tntops.acer.model.Carrier carrier where carrier.fileName=:fileName";
Map map = new HashMap();
map.put("fileName", fileName);
Collection collection = dao.retrieve(hql, map);
if(collection != null && collection.size() > 0){
Iterator it = collection.iterator();
Carrier carrier = (Carrier) it.next();
data = carrier.getContent();

}
//导出成文件
file = new File("d:\\" + fileName);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,则创建
}
fos = new FileOutputStream(file);
int size = 0;
if (data.length > 0) {
fos.write(data, 0, data.length);
} else {
while ((size = in.read(data)) != -1) {
fos.write(data, 0, size);
}
in.close();
}
fos.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值