轉(zhuǎn)自:https://blog.csdn.net/qq_29631069/article/details/70054201 1
一、創(chuàng)建表
2 oracle:
3
4 create table baoxiandan (
5 id number(20) not null,
6 fileName varchar2(200) not null,
7 content blob,
8 primary key(id)
9 );
10 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);
File file = new File(fileNamePath);
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[] {};
data = inputStreamToByte(inputStream);//將文件保存到字節(jié)數(shù)組中
Carrier carrier = (Carrier) SpringContext.getBeanOfType(Carrier.class);
carrier.setFileName(fileName);
carrier.setContent(data);
dao.create(carrier);
//將文件保存到字節(jié)數(shù)組中
private byte [] inputStreamToByte(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();
}
//導(dǎo)出成文件
file = new File("d:\\" + fileName);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,則創(chuàng)建
}
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();
還可以看看
其他文章,謝謝您的閱讀。
網(wǎng)站申明:系本文編輯轉(zhuǎn)載,來源于網(wǎng)絡(luò),目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé),所有權(quán)歸屬原作者。如內(nèi)容、圖片有任何版權(quán)問題,請(qǐng)
聯(lián)系我們刪除。