springMVC+spring+hibernate注解上传文件到数据库,下载,多文件上传

数据库

CREATE TABLE `annex` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `realName` varchar(36) DEFAULT NULL,
  `fileContent` mediumblob,
  `handId` bigint(20) DEFAULT NULL,
  `customerId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_id` (`handId`),
  CONSTRAINT `fk_id` FOREIGN KEY (`handId`) REFERENCES `handprocess` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `handprocess` (
  `id` bigint(20) NOT NULL DEFAULT ‘0‘,
  `handTime` bigint(20) DEFAULT NULL,
  `handName` varchar(20) DEFAULT NULL,
  `reason` varchar(100) DEFAULT NULL,
  `annexStr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

处理过程(handprocess)和附件表(annex)一对多关系,一条处理过程可以有多个附件

除了springmvc+hibernate+spring的jar,还需要

commons-fileupload-1.3.1.jar

commons-io-2.0.1.jar

数据库保存上传文件内容使用mediumblob类型

mysql保存数据库二进制文件使用blob类型,其大小如下

TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G

springmvc中需添加配置文件

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"></property>  <!-- byte -->
        <property name="defaultEncoding" value="utf-8" />
    </bean>

其中maxUploadSizes的大小是上传文件大小,单位:字节

实体:

package com.h3c.zgc.upload;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * 附件     和处理过程多对一
 * @author GoodLuck
 *
 */
@Entity
@Table(name="annex",catalog="itac")
public class Annex {
    @Id
    @Column(name="id")
    private Long id;
    //上传文件的名称
    @Column(name="realName")
    private String realName;
    //上传文件的内容
    @Column(name="fileContent")
    private byte[] fileContent;
    //处理过程的id
    @Column(name="handId")
    private Long handId;
    //客户id
    @Column(name="customerId")
    private Long customerId;

    //getter and setter ...

}
package com.h3c.zgc.upload;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="handprocess",catalog="itac")
public class HandProcess {

    @Id
    @Column(name="id")
    private Long id;
    @Column(name="handTime")
    private Long handTime;
    @Column(name="handName")
    private String handName;
    @Column(name="reason")
    private String reason;
    @Column(name="annexStr")
    private String annexStr;
    @Transient
    private String handTimeStr;

    //getter and setter ...

}

dao层

package com.h3c.zgc.upload;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class UploadDownDao extends HibernateDaoSupport{
    @Resource
    public void set(SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }

    public Session getSession(){
        return this.getSessionFactory().openSession();
    }
    /**
     * 获取附件id最大值
     */
    public Long getMaxIdOfAnnex(){
        List l = this.getHibernateTemplate().find("select max(id) from Annex");
        System.out.println(l);
        if(l!=null&&l.size()==1&&l.get(0)!=null){
            return (Long) l.get(0);
        }
        return 0L;
    }
    /**
     * 获取处理过程id最大值
     * @return
     */
    public Long getMaxIdOfHandProcess(){
        List l = this.getHibernateTemplate().find("select max(id) from HandProcess");
        System.out.println(l);
        if(l!=null&&l.size()==1&&l.get(0)!=null){
            return (Long) l.get(0);
        }
        return 0L;
    }
    /**
     * 保存附件
     * @param annex
     * @return
     */
    public Serializable saveAnnex(Annex annex){
        return this.getHibernateTemplate().save(annex);
    }
    /**
     * 保存处理过程
     * @param hp
     * @return
     */
    public Serializable saveHandProcess(HandProcess hp){
        return this.getHibernateTemplate().save(hp);
    }
    /**
     * 获取处理过程列表,左连接查询
     * @return
     */
    public List getHandProcessList(){//有没有可能没有附件,附件annex,处理过程handprocess
        SQLQuery query = this.getSession().createSQLQuery("select h.id handprocessId,h.annexStr annexStr,a.id annexId,a.realName realName,h.handTime handTime,h.handName handName,h.reason reason from handprocess h left join annex a on a.handId=h.id");
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List list = query.list();
        return list;
    }
    /**
     * 根据id获取附件信息
     * @param annexId
     * @return
     */
    public Annex getAnnexById(Long annexId){
        return this.getHibernateTemplate().get(Annex.class, annexId);
    }
    /**
     * 修改处理过程
     * @param hp
     */
    public void updateHandProcess(HandProcess hp){
        this.getHibernateTemplate().update(hp);
    }
}

service层

package com.h3c.zgc.upload;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

@Service
public class UploadDownService {

    @Resource
    private UploadDownDao uploadDownDao;

    public Long getMaxIdOfAnnex(){
        return this.uploadDownDao.getMaxIdOfAnnex();
    }
    public Long getMaxIdOfHandProcess(){
        return this.uploadDownDao.getMaxIdOfHandProcess();
    }
    @Transactional
    public Serializable saveAnnex(Annex annex){
        return this.uploadDownDao.saveAnnex(annex);
    }
    @Transactional
    public Serializable saveHandProcess(HandProcess hp){
        return this.uploadDownDao.saveHandProcess(hp);
    }
    public Annex getAnnexById(Long annexId){
        return this.uploadDownDao.getAnnexById(annexId);
    }
    public List<Map<String,Object>> getHandProcessList(){
        return this.uploadDownDao.getHandProcessList();
    }
    @Transactional
    public void updateHandProcess(HandProcess hp){
        this.uploadDownDao.updateHandProcess(hp);
    }
}

controller层

package com.h3c.zgc.upload;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadDownFileController {
    @Resource
    private UploadDownService uploadDownService;
    //单文件上传到数据库,MultipartFile中封装了上传文件的信息
    @RequestMapping("upload")
    public String upload(HandProcess hp, HttpServletRequest request,
            @RequestParam("uploadFile1") MultipartFile uploadFile1)
            throws IOException, ParseException {

        InputStream is = uploadFile1.getInputStream();
        byte[] buffer = this.inputStrean2ByteArr(is);
        /**
         * 保存处理过程信息
         */
        // get max id then ++1
        Long hpMaxId = this.uploadDownService.getMaxIdOfHandProcess();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        hp.setHandTime(df.parse(hp.getHandTimeStr()).getTime());
        hp.setId(hpMaxId + 1);
        //保存处理过程
        Serializable hpId = this.uploadDownService.saveHandProcess(hp);
        /**
         * 保存附件
         */
        Annex annex = new Annex();
        annex.setCustomerId(1L);
        annex.setHandId((Long) hpId);
        annex.setRealName(uploadFile1.getOriginalFilename());
        annex.setFileContent(buffer);
        //查找附件id最大值
        annex.setId(this.uploadDownService.getMaxIdOfAnnex() + 1);
        Serializable aid = this.uploadDownService.saveAnnex(annex);
        /**
         * 获取处理过程列表
         */
        List<Map<String, Object>> as = this.uploadDownService
                .getHandProcessList();
        for (Map<String, Object> map : as) {
            map.put("handTime", df.format(map.get("handTime")));
        }
        request.setAttribute("as", as);
        return "annex/annexList";
    }
    /**
     * 多文件上传
     * @param hp   处理过程
     * @param request
     * @param uploadFile1   上传文件
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping("uploadOneMore")
    public String uploadOneMore(HandProcess hp, HttpServletRequest request,
            @RequestParam("uploadFile1") MultipartFile[] uploadFile1)
            throws IOException, ParseException {
        /**
         * 保存处理过程信息
         */
        // get max id then ++1
        Long hpMaxId = this.uploadDownService.getMaxIdOfHandProcess();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        hp.setHandTime(df.parse(hp.getHandTimeStr()).getTime());
        hp.setId(hpMaxId + 1);
        //
        Serializable hpId = this.uploadDownService.saveHandProcess(hp);
        //保存改工单下的所有附件
        for (int i = 0; i < uploadFile1.length; i++) {
            InputStream is = uploadFile1[i].getInputStream();
            byte[] buffer = this.inputStrean2ByteArr(is);

            /**
             * 保存附件
             */
            Annex annex = new Annex();
            annex.setCustomerId(1L);
            annex.setHandId((Long) hpId);
            annex.setRealName(uploadFile1[i].getOriginalFilename());
            annex.setFileContent(buffer);
            annex.setId(this.uploadDownService.getMaxIdOfAnnex() + 1);
            Serializable annexId = this.uploadDownService.saveAnnex(annex);

        }
        //多文件上传,一个处理过程下面要有多个附件,在页面中显示的时候,一个处理过程后面要显示多个附件
        //更新表handprocess
        this.uploadDownService.updateHandProcess(hp);

        List<Map<String, Object>> as = this.uploadDownService
                .getHandProcessList();
        request.setAttribute("as", as);
        return "annex/annexList";
    }

    /**
     * 文件下载
     * @param response
     * @param annexId
     * @throws IOException
     */
    @RequestMapping("download")
    public void download(HttpServletResponse response, Long annexId)
            throws IOException {
        Annex a = this.uploadDownService.getAnnexById(annexId);
        byte[] b = a.getFileContent();
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(a.getRealName().getBytes("gbk"),"iso-8859-1"));  //防止文件乱码

        BufferedOutputStream bos = new BufferedOutputStream(
                response.getOutputStream());
        bos.write(b);
        bos.close();
    }
    /**
     * inputstream转成byte数组
     * @param inStream
     * @return
     * @throws IOException
     */
    public byte[] inputStrean2ByteArr(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

}

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/resources/js/jquery-1.11.1.js"></script>
</head>
<body>
<a href="${pageContext.request.contextPath }/getAllStudent">查找所有用户</a>
<a href="${pageContext.request.contextPath }/savePerson">单独保存Person</a>
<a href="${pageContext.request.contextPath }/savePersonHouse">保存Person和House</a>
<br/>
<form action="${pageContext.request.contextPath }/getArr" >
    <input type="text" name="workSheetId"/><br/>
    <input type="submit" value="search"/>
</form>
<br/><br/><br/>
<form action="<c:url value="/upload"/>" method="post" enctype="multipart/form-data">
    处理时间:<input type="text" name="handTimeStr"/><br>
    处理人:<input type="text" name="handName"/><br/>
    原因:<input type="text" name="reason"/><br/>
    选择文件:<input type="file" name="uploadFile1"/><br/>
    <input type="submit" value="submit"/>
</form>
<br/>
upload one more
<form action="<c:url value="/uploadOneMore"/>" method="post" enctype="multipart/form-data">
    处理时间:<input type="text" name="handTimeStr"/><br>
    处理人:<input type="text" name="handName"/><br/>
    原因:<input type="text" name="reason"/><br/>
    选择文件:<input type="file" name="uploadFile1"/><br/>
    选择文件:<input type="file" name="uploadFile1"/><br/>
    选择文件:<input type="file" name="uploadFile1"/><br/>
    <input type="submit" value="submit"/>
</form>
</body>
</html>

annexList.jsp  显示处理过程已经处理过程下附件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
    src="${pageContext.request.contextPath }/resources/js/jquery-1.11.1.js"></script>
</head>
<body>
    <table>
        <c:forEach items="${as }" var="ah" varStatus="status">
            <tr>
                <td>${ah[‘handprocessId‘] }</td>
                <td>${ah[‘handName‘] }</td>
                <td>${ah[‘reason‘] }</td>
                <td id="id${status.index }">${ah[‘handTime‘] }</td>

                <td>
                    ${ah[‘annex‘][‘realName‘] }
                    <a href="<c:url value="/download?annexId=${ah[‘annexId‘] }"/>">${ah[‘realName‘] }</a>
                </td>
            </tr>
        </c:forEach>

    </table>
</body>
<script type="text/javascript">

</script>
</html>
时间: 2024-10-26 02:55:01

springMVC+spring+hibernate注解上传文件到数据库,下载,多文件上传的相关文章

用Maven整合SpringMVC+Spring+Hibernate 框架

用Maven整合SpringMVC+Spring+Hibernate 框架, 实现简单的插入数据库数据 一.搭建开始前的准备 1.打开MyEclipse新建Maven项目.File>New>Other(或Ctrl+N)>Maven Project:然后我们用default Workspace就行了(注意Location的路径,区分目录名和项目名):然后再Filter中输入webapp,我们选择org.apache.maven.archetypes 下的maven-archetype-we

SpringMVC+Spring+Hibernate的小样例

Strusts2+Spring+Hibernate尽管是主流的WEB开发框架,可是SpringMVC有越来越多的人使用了.确实也很好用.用得爽! 这里实现了一个SpringMVC+Spring+Hibernate的小样例.凝视都在代码里面. 项目各包的结构例如以下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlhbnR1amF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve

Maven搭建springMVC+spring+hibernate环境

这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apa

SpringMVC+Spring+Hibernate的小例子

Strusts2+Spring+Hibernate虽然是主流的WEB开发框架,但是SpringMVC有越来越多的人使用了,确实也非常好用,用得爽! 这里实现了一个SpringMVC+Spring+Hibernate的小例子.注释都在代码里面. 项目各包的结构如下图: 1, 首先是pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLS

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能(二)

前一篇写的有些多,大家先看前一篇,传送门 具体的资源已将上传到资源了. 附地址:MySQL.zip启动 用Maven整合SpringMVC+Spring+Hibernate 框架 上文我们直接搭建前的准备和资源配置都写好了,下面进入具体代码编写.承接上文的小3 3.我习惯建立接口,这样对整个项目感觉更合理. (1.)建立IBaseService(业务逻辑层,有的习惯写成BaseServiceI)都可以,都是标注接口的,我只是简单的贴下代码 package com.jesus.sshframewo

Springmvc+Spring+Hibernate搭建方法及实例

Springmvc+Spring+Hibernate搭建方法及实例

spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件

这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件 1.web.xml 要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下: <?xml version="1.0" encoding= "UTF-8"?> <web-app version= "3.0" xmlns="http://java.sun.com/xml/n

springMVC+spring+hibernate 框架整合实例

先说一下流程思路: 流程讲解1:首先访问会先定位到控制器.这就用到了过滤器配置文件"spring-mvc.xml".这个文件负责定义控制器的包路径.视图的格式等.其次从"控制器->service层->dao层",期间用的都是spring的依赖注入,所以就需要一个bean容器配置文件,将所有的"等待被注入的类"在xml文件里申明一下.这个xml文件就是"spring-beans.xml".之后,dao层要操作enti

JAVA代码实现下载单个文件,和下载打包文件

//下载单个文件调用方法 /**     * response     * imgPath 下载图片地址    * fileName 保存下载文件名称    * @date 2015年4月14日 下午5:53:24     */     public static void download(HttpServletResponse response,String imgPath,String fileName){         OutputStream out=null;         Bu