oracle,weblogic读写blob

一 开发环境:XP+Myeclipse6.6+Weblogic92
二 上传组件: smart-upload
三  示例代码
1 JSP代码
<%@ page language="java" pageEncoding="UTF-8"%>
<form name=form1 method=post enctype="multipart/form-data" action="/testFile">
<input type="file" name="f" id="f"/>
<img src="" id="img" name="img" width="100%" border="0" height="100%"/>
 </form>

2 java代码:保存文件到DB与读取

request.setCharacterEncoding("UTF-8");
try {
			com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.SmartUpload();
			mySmartUpload.initialize(getServletConfig(), request, response);
			mySmartUpload.upload();
			com.jspsmart.upload.Request rq = mySmartUpload.getRequest();
			byte[] bytes = null;
			//读取上传文件的字节码
			for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
				com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
				if (!myFile.isMissing()) {
					int fileLength = myFile.getSize();					
					bytes = new byte[fileLength];
					for (int j = 0; j < fileLength; j++) {
						bytes[j] = myFile.getBinaryData(j);
					}
				}
			}
			//保存到DB
			if(null != bytes){
				      try {
					java.sql.Connection conn = DbUtils.getConn();
					//test表的fcontext字段类型为:BLOB
					String sql = "update test set fcontext=empty_blob()";
					Statement stmt = conn.createStatement();
					stmt.executeUpdate(sql);			
					sql = "select fcontext from test FOR UPDATE";
					PreparedStatement ps = conn.prepareStatement(sql);					
					ps.executeUpdate();
					java.sql.ResultSet rs = ps.getResultSet();
					if (rs.next()) {
						if (bytes.length > 0) {
							weblogic.jdbc.vendor.oracle.OracleThinBlob content = (weblogic.jdbc.vendor.oracle.OracleThinBlob) 
								rs.getBlob("fcontext");
							OutputStream outstream = content.getBinaryOutputStream();
							try {
								outstream.write(byteContent, 0, byteContent.length);
								outstream.flush();
								outstream.close();
							} catch (IOException e) {
								e.printStackTrace();
							}

						}
					}
					returnValue = true;
					DbUtils.close(rs);
					DbUtils.close(ps);
					DbUtils.close(stmt);
					DbUtils.close(conn);
				} catch (SQLException ex) {
						ex.printStackTrace();
				}
			}
			//从DB中读取
			bytes  = new byte[0];
			try {
				String sql = "select fcontext from test";
				java.sql.Connection conn = DbUtils.getConn();
				PreparedStatement ps = conn.prepareStatement(sql);					
				ps.executeUpdate();
				java.sql.ResultSet rs = ps.getResultSet();
				if(rs.next()){
					java.sql.Blob bolbObject = rs.getBlob("fcontext");
					if (bolbObject != null) {
						int length = (int) bolbObject.length();
						BufferedInputStream inStream = new BufferedInputStream(bolbObject.getBinaryStream());
						returnValue = new byte[length];
						inStream.read(bytes);	        
						inStream.close();
					  }
				}
				DbUtils.close(rs);
				DbUtils.close(ps);
				DbUtils.close(conn);			
			} catch (Exception ex) {			
				ex.printStackTrace();
			}finally{
				//关闭资源
			}

		}catch (SmartUploadException e) {
			e.printStackTrace();
		}
	}


3 读取DB字节数组并映射为图片格式:在web.xml添加相应映射:/ readImg

class ReadDbImage extends HttpServlet 
{

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	    java.io.ByteArrayInputStream in = null;
		java.io.OutputStream outStream = null;		
		// 禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		//调用DAO类读取Blog为字节数组
		byte[] fcontext = getFilContext();			
		if (fcontext != null) {			
			response.reset();			
			in = new ByteArrayInputStream(fcontext);
			outStream = response.getOutputStream();
			byte[] buf = new byte[1024];
			int bytes = 0;
			while((bytes = in.read(buf)) != -1)
				outStream.write(buf, 0, bytes);
			in.close();
			outStream.close();
			outStream.flush();
			in = null;
			outStream = null;
		}
	}
	
}


4  JS中操作读取DB图片
document.getElementById("img").src="/readImg?time="+new Date();

5 清除附件框值的组件
clearFile.js见附件,中有相应示例

你可能感兴趣的:(java,oracle,sql,weblogic,XP)