java上传⽂件-⼤⽂件以⼆进制保存到数据库
转⾃:blog.csdn/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        "hibernate.sourceforge/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="del.impl.CarrierImpl"
8          table="BAOXIANDAN"
9          proxy="del.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.de}</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>
2
3
4 del.impl.BaseModelImpl;
5 del.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        t = content;
44    }
45 }
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) BeanOfType(Carrier.class);
10            carrier.setFileName(fileName);
11            carrier.setContent(data);
12            ate(carrier);
meta大裁员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 [] =ByteArray();
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 del.Carrier carrier where carrier.fileName=:fileName";
33        Map map = new HashMap();
34        map.put("fileName", fileName);
35        Collection collection =  ieve(hql, map);
36        if(collection != null && collection.size() > 0){
37            Iterator it = collection.iterator();
38            Carrier carrier = (Carrier) it.next();
39            data = Content();
40
41        }
42 //导出成⽂件
43        file = new File("d:\\" + fileName);
44        if (!ists()) {
45            ateNewFile(); // 如果⽂件不存在,则创建
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();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。