BLOB的读写操作
//BLOB写入操作
package zxt.xsfw.action.ceshi; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import zxt.wai.action.ProtectedAction;
import zxt.wai.sys.Constant;
import zxt.wai.sys.RequestHelper;
import zxt.xsfw.init.WebInitLoad; import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException; import oracle.sql.BLOB;
import java.sql.Statement; public class BlobceshiAction extends ProtectedAction{ public boolean postProcess(HttpServletRequest request, HttpServletResponse response) {
return false;
} public boolean process(HttpServletRequest request, HttpServletResponse response) {
String name = RequestHelper.getStrParam(request, "NAME");//请求中读取参数
String img = RequestHelper.getStrParam(request, "IMG");//请求中读取参数 Connection conn=null;
Statement stm=null;
ResultSet rs=null;
BLOB blob = null;
FileInputStream fin=null;
OutputStream out=null;
try{
conn = WebInitLoad.getSysDBTookit().createConnection();
stm = conn.createStatement();
conn.setAutoCommit(false);
String sql = "insert into TEST values('1','"+name+"',EMPTY_BLOB())";
stm.executeUpdate(sql); rs = stm.executeQuery("SELECT img FROM TEST WHERE id='1' FOR UPDATE "); fin = new FileInputStream(img);
byte[] blobBuf = new byte[(int)fin.available()];
fin.read(blobBuf);
fin.close(); if(rs.next()) {
System.out.println(blobBuf.length);//以byte为单位
blob = (oracle.sql.BLOB)rs.getBlob(1);
out = blob.getBinaryOutputStream();
out.write(blobBuf);//写入到BLOB中去
out.close();
conn.commit();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
setReturnValue("<script>alert(\"ok!BLOB保存成功!\");history.back();</script>");
setReturnType(Constant.TARGET_TYPE_CONTENT);
return false;
} }
//BLOB写入操作
package zxt.xsfw.view.ceshi;
import org.enhydra.xml.xmlc.html.HTMLObject;
import zxt.wai.sys.XmlcViewFactory;
import zxt.wai.view.ProtectedXView;
import zxt.xsfw.init.WebInitLoad;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.sql.BLOB;
public class ReadBlobceshiXView {
public boolean render() {
Connection conn=null;
Statement stm=null;
ResultSet rs=null;
try {
conn = getCon();
stm = conn.createStatement();
rs = stm.executeQuery("select img from TEST where id='1'");
if(rs.next()){
BLOB blob = (BLOB)rs.getBlob(1);
InputStream is = blob.getBinaryStream();
FileOutputStream os = new FileOutputStream("c:\\1.jpg");
int b;
byte[] buffer = new byte[1024];
while( (b=is.read(buffer)) != -1){
os.write(buffer,0,b);//把BLOB信息写到文件流中
}
is.close();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public static void main(String[] orgs){
new ReadBlobceshiXView().render();
}
public static Connection getCon() {
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:ora9i";
String user = "";
String password = "";
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
BLOB的读写操作的更多相关文章
- oracle读写文件--利用utl_file包对磁盘文件的读写操作
oracle读写文件--利用utl_file包对磁盘文件的读写操作 摘要: 用户提出一个需求,即ORACLE中的一个表存储了照片信息,字段类型为BLOB,要求能导出成文件形式. 本想写个C#程序来做, ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- [转]Android - 文件读写操作 总结
转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...
- App.Config详解及读写操作
App.Config详解及读写操作 App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而 ...
- 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)
算是一个小技巧吧,只是进行一些简单的读写操作.让人不爽的是xlrd和xlwt是相对独立的,两个模块的对象不能通用,读写无法连贯操作,只能单独读.单独写,尚不知道如何解决. #①xlrd(读) #cod ...
- io流对文件读写操作
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedRead ...
- PHP文件读写操作之文件写入代码
在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...
- INI 文件的读写操作
在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
随机推荐
- 8 Reasons why SharePoint is Bad for Your Business 8个理由告诉你,为什么SharePoint对你的业务有害
8 Reasons why SharePoint is Bad for Your Business 8个理由告诉你,为什么SharePoint对你的业务有害 SharePoint近期已 ...
- java学习笔记(二)图形用户接口
这个学期主要放在ACM比赛上去了,比赛结束了.不知不觉就15周了,这周就要java考试了,复习一下java吧.java的学习的目的还是让我们学以致用,让我们可以运用java开发一下小项目.而不是单单应 ...
- Tomcat启动时项目反复载入,导致资源初始化两次的问题
近期在项目开发測试的时候,发现Tomcat启动时项目反复载入,导致资源初始化两次的问题 导致该问题的解决办法: 例如以下图:在Eclipse中将Server Locations设置为"Us ...
- Controller methods and views
https://docs.asp.net/en/latest/tutorials/first-mvc-app/controller-methods-views.html We have a good ...
- div向右偏移设置 css让div靠右移一定距离
转自:https://www.thinkcss.com/shili/1372.shtml div对象盒子向右偏移设置,使用css让div靠右一定距离-div向右移教程实例篇 div向右偏移一定距离,可 ...
- 【转】什么是P问题、NP问题和NPC问题
原文链接:http://www.matrix67.com/blog/archives/105,感谢Matrix67,看完这篇文章终于把这个几个概念弄明白了!! 这或许是众多OIer最大的误区之一. ...
- BZOJ 2002 LCT板子题
思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...
- hdu2112 HDU Today 基础最短路
这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...
- [转]C++内存管理
[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...
- PCL:全程详解 VS2010+PCL配置
浑浑噩噩半年时间,终于不得不干点和机器人有关的东西.ICRA和IROS推荐的三维图形库-点云库,几乎成了机器人视觉算法的标配. 参考了几篇文章,最后终于配置成功. 下面是文章地址: 这个有点看头,累积 ...