文件上传存至oracle,fileupload上传文件存储到oracle Blob字段中
从网上查了许多资料,攒了个功能,暂且记下来,呵呵。需要apache的fileupload组件和io组件。
// 建立一个新的Upload对象
DiskFileUpload upload = new DiskFileUpload();
upload.setSizeThreshold(1024 * 300); //设置缓冲区大小
String rootPath = getServletConfig().getServletContext().getRealPath("/");
upload.setRepositoryPath(rootPath+"//uploads");//设置缓冲区目录upload.setSizeMax(10*1024*1024); //限制最大文件为10MB FileItem item = null;Connection conn = null; ResultSet rst = null;PreparedStatement pstmt = null;OutputStream output = null;BufferedInputStream bi = null;try{ List items = upload.parseRequest(request); Iterator iter = items.iterator(); while(iter.hasNext()){ item = (FileItem) iter.next(); if (!item.isFormField()){//(不是表单域)是文件对象 bi = new BufferedInputStream(item.getInputStream()); byte[] buffer = new byte[1024]; conn = JdbcUtilsHelper.getConnection(); conn.setAutoCommit(false); //先清空原来的上传文件 pstmt = conn.prepareStatement("update table set col=EMPTY_BLOB() where id=?"); pstmt.setString(1, xh); pstmt.execute(); pstmt.close(); pstmt = conn.prepareStatement("select col from table where id=? for update"); pstmt.setString(1, xh); rst = pstmt.executeQuery(); if(rst.next()){ System.out.println("next"); oracle.sql.BLOB blob = (oracle.sql.BLOB)rst.getBlob(1); output = blob.setBinaryStream(0); int readNum = -1; while( (readNum = bi.read(buffer)) != -1){ //通过Oracle驱动使用流方式读写 output.write(buffer,0,readNum); } //BLOB字段输出流关闭,以及其他GC处理 bi.close(); output.flush(); output.close(); conn.commit(); rst.close(); item.delete(); } conn.setAutoCommit(true); } }// end while rtnCode = "1";//保存成功}catch(FileUploadException e) {// 处理文件尺寸过大异常 if(e instanceof SizeLimitExceededException) { rtnCode = "-2";//文件太大 } e.printStackTrace(); }catch(Exception e){ rtnCode = "-1";//保存失败 e.printStackTrace();}finally{ if(rst != null){ try{ rst.close(); }catch(Exception e1){ e1.printStackTrace(); } } if(pstmt != null){ try{ pstmt.close(); }catch(Exception e1){ e1.printStackTrace(); } } if(conn != null){ conn.setAutoCommit(true); try{ conn.close(); }catch(Exception e1){ e1.printStackTrace(); } } if(item != null){ item.delete(); } }// end finally
文件上传存至oracle,fileupload上传文件存储到oracle Blob字段中相关推荐
- 打开dll文件时模块加载失败,请确保二进制存储在指定的路径中,或者调试它以检查该二进制或相关的.DLL文件是否有问题
** 打开dll文件时模块加载失败,请确保二进制存储在指定的路径中,或者调试它以检查该二进制或相关的.DLL文件是否有问题 **打开dll文件时模块加载失败,电脑提示请确保二进制存储在指定的路径中,或 ...
- 使用()控件的saveas方法可以将上传文件保存到服务器.,NET开发-FileUpload上传控件HasFile、FileName、SaveAs()的使用...
.NET开发-FileUpload控件 1. FileUpload控件概述 在ASP.NET WebForm的标准控件中,可以使用FileUpload控件实现客户端文件(任意类型的文件)的上传,上传后 ...
- servlet文件上传blob_servlet实现从oracle数据库的blob字段中读出文件并显示 | 学步园...
/* * ReadFile.java * * Created on 2004年11月17日, 下午3:41 */ package jgwl.app; import java.io.*; import ...
- 图片上传到数据库,保存到Blob字段中
需求:从系统上传图片,将图片信息保存到数据库的blob字段中,并提供照片的查询接口 controller: package com.success.controller;import ../*** 系 ...
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中
转自:http://blog.csdn.net/w59879213/article/details/7162769 开发心得. 首先是获取网络中的图片, 将图片从网络中以NSData的形式读取到本地的 ...
- oracle查询blob模糊搜索,BLOB字段模糊查询
1. BLOB字段模糊查询报错 blob字段直接用select * from table_name where column like'%%'查找的时候是不能实现的,主要是字段类型不符.不 ...
- 使用JDBC操作基于Oracle的CLOB,BLOB字段类型
转载至程式先锋技术维客[url]www.javabiz.cn[/url] 先来看看两种LOB类型的主要区别: CLOB 表示Character LOB (字符LOB) 它可以存储大量的字符数据. B ...
- java向Oracle数据库中插入blob字段数据
java不能直接想插入其他普通类型数据一样插入blob字段数据,因为blob相当于一个大文件块,里面有游标,需要初始化blob游标才能插入数据.所以我们要先插入一个空的blob数据,以初始化游标,然后 ...
- Mybatis操作Oracle中的Clob和Blob字段
文章目录 Mybatis操作Oracle中的Clob和Blob字段 [ 我测试用的Mybatis Plus ] A.数据准备 A-1. Oracle中创建测试的表结构:Byte_Array_Test, ...
最新文章
- gradle仓库配置
- Apple Watch开发快速入门教程
- CentOS 7下搭建配置 SVN 服务器
- linux下配置vsftpd虚拟用户为登录用户
- 这才是真正的 Git——分支合并
- 使用jasmine.createSpyObj具有依赖关系的Angular服务进行单元测试
- (ssh整合web导出excel)在ssh框架中使用poi正确导出具有比较高级固定格式的excel 整体过程,查询导出前后台下载...
- js获取url传递得参数
- C#开源爬虫NCrawler源代码解读以及将其移植到python3.2(3)
- Nanohttpd 异常 Explicit termination medthod 'end' not called 解决方法
- 程序员学英语Oeasy
- 蠢蛋秀:匠心独运的脑筋急转弯游戏
- BZOJ 1106: [POI2007]立方体大作战tet
- SEO优化收徒蜘蛛池是什么
- 支付宝、微博牛人专家来袭!详解春节红包技术(全程直播)
- C++跨平台开源库 之二
- 选择语句switch和循环语句while
- 2022-2027年中国海洋功能性食品行业发展监测及投资战略研究报告
- vertical-align middle 不生效的几种解决方式,图片居中几种方案
- 华为交换机重置console密码(S3700)