任务:

uos.docfile的content字段是longblob类型的,通过页面将文件存储到这个字段里。

页面代码

<div class="box">
    <div class="box-head">
        <h2>Upload a document</h2>
    </div>

    <form name="form1" action="uploadDocument.html" method="post" ENCTYPE="multipart/form-data">
    <div class="form" >
            <p>
                <span class="req"><input id="remarkTxt"  name="remarkTxt" class="field size4" title="Enter the date" /></span>
                <label>Remark: <span>(The brief introduction of the file)</span></label>
            </p>

            <p>
                <span class="req"><input type="file"  name="uploadFileCtrl" class="field size4" title="Choose the file" /></span>
                <label>Upload file: <span>(Max Size:20M)</span></label>
            </p>
    </div>

    <div class="buttons">
        <input id="queryBtn" type="button" class="button" value="Submit" />
    </div>

    </form>
</div>

1.控制器的代码

    @RequestMapping(value="/uploadDocument")
    public String uploadDocument(@RequestParam("remarkTxt") String remark,
                                  @RequestParam("uploadFileCtrl") MultipartFile file,HttpServletRequest request,HttpServletResponse response){
        try {
            // 从session中获得用户
            String userId=getUserIdFromSession(request);

            // 得到上传文件名
            String uploadFileName=file.getOriginalFilename();
            request.setAttribute("uploadFileName", uploadFileName);

            if(file.isEmpty()==false){
                InputStream is=file.getInputStream();

                service.uploadDocument(remark, is,file.getSize(),userId,uploadFileName);

                is.close();

                return "/pages/doc/result/index.jsp";
            }else{
                throw new Exception("The file you uploaded is NULL. Please check and retry.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);

            request.setAttribute("error", e.getClass());
            request.setAttribute("reason", e.getMessage());
            StackTraceElement[] arr=e.getStackTrace();
            request.setAttribute("stackTraceElements", arr);

            return "pages/error/index.jsp";
        }
    }

2.Serivce中代码,这部分只是个中转

    public int uploadDocument(String remark,InputStream fin,long filesize,String email,String uploadFileName) throws Exception{
        return getPosDao().uploadDocument(remark, fin,filesize,email,uploadFileName);
    }

3.DAO中代码,这部分是实质性代码

    public int uploadDocument(final String remark,final InputStream fin,final long filesize,final String email,String uploadFileName) throws Exception{
        final LobHandler lobHandler=new DefaultLobHandler();

        return this.getJdbcTemplate().execute("insert into uos.docfile(remark,content,email,addtime,filename) values ('"+remark+"',?,'"+email+"',NOW(),'"+uploadFileName+"' )",
            new AbstractLobCreatingPreparedStatementCallback(lobHandler){
                    protected void setValues(PreparedStatement pstmt,LobCreator lobCreator){
                        try {
                            lobCreator.setBlobAsBinaryStream(pstmt,1,fin,(int)filesize);
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                     }
            });
    }
												

SpringMVC处理MYSQL BLOB字段的上传的更多相关文章

  1. SpringMVC处理MySQL BLOB字段的下载

    任务: uos.docfile的content字段是longblob类型,通过Web点击链接能下载到存储在这个字段里的文件.Web点击链接类似如下形式: http://localhost:8080/d ...

  2. SpringMVC结合ajaxfileupload文件无刷新上传

    jQuery没有提供ajax的文件上传,我们可以通过ajaxfileupload实现ajax文件的上传.其实ajaxfileupload文件上传特别的简单.下面就演示一下在SpringMVC中实现aj ...

  3. spring--mvc添加用户及用户头像上传

    spring--mvc添加用户及用户头像上传 添加用户步骤: 1.用ajax获取省份信息 2.添加用户 代码:register.jsp <meta http-equiv="Conten ...

  4. JavaScript把项目本地的图片或者图片的绝对路径转为base64字符串、blob对象在上传

    主题: JavaScript把项目本地的图片或者图片的绝对路径转为base64字符串.blob对象在上传. 用处: 从本地选择图片上传,如项目规定只能选择本项目文件夹下的图像上传为头像等. 主要思想: ...

  5. 阿里云CentOS自动备份MySql 8.0并上传至七牛云

    本文主要介绍一下阿里云CentOS7下如何对MySql 8.0数据库进行自动备份,并使用.NET Core 将备份文件上传至七牛云存储上,并对整个过程所踩的坑加以记录. 环境.工具.准备工作 服务器: ...

  6. SpringMVC(四)-- springmvc的系统学习之文件上传、ajax&json处理

    资源:尚学堂 邹波 springmvc框架视频 一.文件上传 1.步骤: (1)导入jar包 commons-fileupload,commons-io (2)在springmvc的配置文件中配置解析 ...

  7. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  8. js bese64转化为blob使用FormData上传

    原文 工作示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  9. SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传

    1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...

随机推荐

  1. 向mysql中批量插入数据的性能分析

    MYSQL批量插入数据库实现语句性能分析 假定我们的表结构如下 代码如下   CREATE TABLE example (example_id INT NOT NULL,name VARCHAR( 5 ...

  2. transition transform animate的使用

    注:代码显示效果可以自行粘贴复制查看 transition(过渡),主要是关注property的变化主要有四个属性transition-property.transition-durantion.tr ...

  3. 为Ubuntu Gnome环境创建桌面快捷方式

    为Ubuntu Gnome环境创建桌面快捷方式 安装gnome-panel包 sudo apt-get install --no-install-recommends gnome-panel 启动终端 ...

  4. Bzoj2165 大楼

    Time Limit: 40 Sec  Memory Limit: 259 MBSubmit: 779  Solved: 285[Submit][Status][Discuss] Descriptio ...

  5. ClientScript.RegisterClientScriptBlock 不执行

    ClientScript.RegisterClientScriptBlock 不执行 页面中 form标签必须加入 runat=server

  6. 废弃sqlite代码,备查

    using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using T ...

  7. Cow Navigation(USACO)

    题目大意: 给定一个N*N的矩阵,并告诉你每一个格子能否经过,要求你求出(n,1)到(1,n)的最短路径的操作数. 其中操作有2种,第一种是沿着目前的方向走一格,第二种是向左或向右转90° 由于题目设 ...

  8. (转)Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

    刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...

  9. SPI总线介绍

    1. 简介 SPI, Serial Peripheral Interface, 串行外设接口, 是一种高速的.全双工.同步的通信总线SPI在芯片的管脚上只占用四根线 SPI接口主要用于MCU与各种外围 ...

  10. 2018年最重要的HTML5开发手册,传播正能量

    今天给大家推荐这个HTML5开发手册,希望能帮助正在学习web前端的人,鄙人也是刚学习前端没多久,借助于一点资讯平台能够结识更多前端大牛,这是我的web前端/HTML5/javscript技术学习群: ...