BLOB的读写操作

最近学习了一些BLOB读写的操作,用的是java io的操作,读取文件信息写到BLOB中,再从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;
	}

}

 

你可能感兴趣的:(oracle,blob)