打印本文 打印本文 关闭窗口 关闭窗口
插入图片/文本(blob /clob)到oracle数据库
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1886  更新时间:2009/4/22 22:03:56  文章录入:mintao  责任编辑:mintao
  out1.close();

通过缓冲一个个读数据,然后一个个写数据.-1 为文件的末尾,

最后当读写完成后我们要关闭读写对象!

程序分析就是这样,以后还要对此问题进行研究,最后还要注意,

<%@ page contentType="image/jpeg;charset=GBK"%>

不是

<%@ page contentType="text/html;charset=GBK"%>

否则是以文字显示图片---乱码.

 

这里研究了上传图片到oralce 里面的程序,关于显示还要麻烦一点,借助资料我实现了,明天再研究一下.

 

 //插入上传图片到数据库
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="oracle.sql.*"%>
<html>
 
 
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>getPicture.jsp</title>
  </head>
 
 
 
 
  <body>
 
  <%
  request.setCharacterEncoding("GBK");
   
 
  String name=null;
  long size=0;
 
  Connection conn=null;
  String insert=" insert into uploadpicture "+
                " values(?, empty_blob()) "     ;
 
  String findCursor=" select content "+
                    " from uploadpicture "+
                    " where name=?  for update ";
 
  String update=" update uploadpicture  "+
                " set content=? "+
                " where name=? ";
  BLOB blob=null;
    InputStream is=null;
 
  DiskFileUpload dfu=new DiskFileUpload();
  dfu.setSizeMax(100000000);
  dfu.setSizeThreshold(100000);
  dfu.setRepositoryPath("f:\\public");
 
  try{
  List fileItems=dfu.parseRequest(request);
  Iterator i=fileItems.iterator();
 
  while(i.hasNext()){
  FileItem fi=(FileItem)i.next();
  if(!fi.isFormField()){
  name=fi.getName();                   
  size=fi.getSize();                    
  if((name==null||name.equals(""))&&size==0)
  continue;
                        }
  name=fi.getName();
  size=fi.getSize();
  is=fi.getInputStream();
 
                    }
                   
  Context ctx=new InitialContext();
  DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
  conn=ds.getConnection();
  conn.setAutoCommit(false);
 
  //step 1
  PreparedStatement ps=conn.prepareStatement(insert);
  ps.setString(1, name);
  int a=ps.executeUpdate();
  if(a>0)
  out.println("insert success!"+"<br>");
 
  //step 2
  ps=conn.prepareStatement(findCursor);
  ps.setString(1, name); 
  ResultSet rs=ps.executeQuery();
  while(rs.next())
  {
  blob=(BLOB)rs.getBlob(1);
 
 
   out.println("find cursor success!"+"<br>");
   out.println("cursor            :"+blob+"<br>");
  //step 3
  ps=conn.prepareStatement(update);
  ps.setBlob(1, blob);
  ps.setString(2, name);
  ps.executeUpdate();
  ps.close();
  BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());
  BufferedInputStream in=new BufferedInputStream(is);
  int c;
  while((c=in.read())!=-1) {out1.write(c);}
  in.close();
  out1.close();
  out.println("update success!"+"<br>");}
  conn.commit();
  }
 
  catch(SQLException se)
  {se.printStackTrace();}
  catch(FileUploadException fue)
  {fue.printStackTrace();}
  %>
 
 
  </body>

</html>


//显示数据库里面的图片

<%@ page contentType="image/jpeg;charset=GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.image.*"%>


<html>
 
 
 
  <head>
    <meta http-equiv="Content-Type" content="image/jpeg; charset=GBK">
    <title>showDBImage.jsp</title>
  </head>
 
 
 
  <body>
  <%
  String showImage=" select * "+
                   " from uploadpicture "+
                   " where name=''''TXC with snow.JPG'''' " ;
  Connection conn=null;
  BufferedInputStream inputImage=null;
 
 
  try{
  Context ctx=new InitialContext();
  DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");
  conn=ds.getConnection();
  Statement st=conn.createStatement();
  ResultSet rs=st.executeQuery(showImage);
  while(rs.next())
  {
  oracle.sql.BLOB blob=(oracle.sql.BLOB)rs.getBlob("content");
  inputImage =new BufferedInputStream(blob.getBinaryStream());
  /*String name=rs.getString(1);
  String content=rs.getString(2);
  out.println(name+"<br>");*/}
 
  BufferedImage image=null;
  image=ImageIO.read(inputImage);
 
  ServletOutputStream sos=response.getOutputStream();
  JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
  encoder.encode(image);
  inputImage.close();
  conn.commit();
 
  }
  catch(SQLException se)
  {se.printStackTrace();
  conn.rollback();  }
  catch(IOException ie)
  {ie.printStackTrace();} 
  %>
 
  </body>


</html>

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口