java上传文件-大文件以二进制保存到数据库

转自:https://blog.csdn.net/qq_29631069/article/details/70054201
1
一、创建表 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;
 1 二、Hibernate映射文件
 2 
 3 <?xml version="1.0" encoding="utf-8"?>
 4 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 5         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 6 <hibernate-mapping>
 7     <class name="com.erry.tntops.acer.model.impl.CarrierImpl"
 8            table="BAOXIANDAN"
 9            proxy="com.erry.tntops.acer.model.Carrier">
10         <id name="id" column="ID" type="long" unsaved-value="-1">
11             <generator class="sequence">
12                 <param name="sequence">SEQ_BAOXIANDAN</param>
13             </generator>
14         </id>
15         <property name="fileName" column="filename" type="java.lang.String" not-null="false">
16             <meta attribute="caption">${tntops.acer.Carrier.code}</meta>
17         </property>
18         <property name="content" column="content" type="byte[]">
19             <meta attribute="caption">${tntops.acer.Carrier.name}</meta>
20         </property>
21     </class>
22 </hibernate-mapping> 
 1 三、pojo类
 2 
 3 
 4 import com.erry.tntops.common.model.impl.BaseModelImpl;
 5 import com.erry.tntops.acer.model.Carrier;
 6 
 7 import java.util.Date;
 8 
 9 /**
10 * Created by IntelliJ IDEA.
11 * User: chenyang
12 * Date: 2008-7-28
13 * Time: 17:46:32
14 * To change this template use File | Settings | File Templates.
15 */
16 public class CarrierImpl extends BaseModelImpl implements Carrier {
17 
18     private long id;
19     private String fileName;
20     private byte content[];
21 
22     public long getId() {
23         return id;
24     }
25 
26     public void setId(long id) {
27         this.id = id;
28     }
29 
30     public String getFileName() {
31         return fileName;
32     }
33 
34     public void setFileName(String fileName) {
35         this.fileName = fileName;
36     }
37 
38     public byte[] getContent() {
39         return content;
40     }
41 
42     public void setContent(byte[] content) {
43         this.content = content;
44     }
45 }
 1 四、Java代码
 2 1、入库
 3             //获得文件名,fileNamePath为文件路径变量
 4             String fileName = fileNamePath.substring(fileNamePath.lastIndexOf("\") + 1);
 5             File file = new File(fileNamePath);
 6             InputStream inputStream = new FileInputStream(file);
 7             byte[] data = new byte[] {};
 8             data = inputStreamToByte(inputStream);//将文件保存到字节数组中
 9             Carrier carrier = (Carrier) SpringContext.getBeanOfType(Carrier.class);
10             carrier.setFileName(fileName);
11             carrier.setContent(data);
12             dao.create(carrier);
13 
14 //将文件保存到字节数组中
15     private byte [] inputStreamToByte(InputStream is) throws IOException {
16         ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
17         int ch;
18         while((ch = is.read() ) != -1){
19             bAOutputStream.write(ch);
20         }
21         byte data [] =bAOutputStream.toByteArray();
22         bAOutputStream.close();
23         return data;
24     }
25 
26 2、出库
27 byte data [] = new byte[]{};
28         File file =null ;
29         FileOutputStream fos = null;
30         InputStream in = null;
31 
32         String hql = "select carrier from com.erry.tntops.acer.model.Carrier carrier where carrier.fileName=:fileName";
33         Map map = new HashMap();
34         map.put("fileName", fileName);
35         Collection collection =  dao.retrieve(hql, map);
36         if(collection != null && collection.size() > 0){
37             Iterator it = collection.iterator();
38             Carrier carrier = (Carrier) it.next();
39             data = carrier.getContent();
40 
41         }
42 //导出成文件
43         file = new File("d:\" + fileName);
44         if (!file.exists()) {
45             file.createNewFile(); // 如果文件不存在,则创建
46         }
47         fos = new FileOutputStream(file);
48         int size = 0;
49         if (data.length > 0) {
50             fos.write(data, 0, data.length);
51         } else {
52             while ((size = in.read(data)) != -1) {
53                 fos.write(data, 0, size);
54             }
55             in.close();
56         }
57         fos.close(); 
原文地址:https://www.cnblogs.com/sharpest/p/5987885.html