`
czllfy
  • 浏览: 107191 次
  • 来自: ...
社区版块
存档分类
最新评论

图片上传到oracle数据库的blob字段

阅读更多

Ry_jbxx_list.jsp
<%String href_edit="window.open('"+linkinfoURL+"TASK=edit&RY_ID="+jatt.RY_ID+"&RYLXBZ="+(String)request.getAttribute("RYLXBZ")+"&DWLX="+dwlx+"','ry_jbxx_edit','left=5,top=10,height=480,width=900,menubar=no,resizable=yes,location=no,toolbar=no,scrollbars=yes')";%>

Ry_jbxx_edit.jsp

在javascript代码区中增加两个函数
function pic_change()
{
document.ry_jbxx_ActionForm.show_picture.src=document.ry_jbxx_ActionForm.PIC.value;
((document.ry_jbxx_ActionForm.show_picture.fileSize/1024).toFixed(2)>1)?alert("文件大小:"+(document.ry_jbxx_ActionForm.show_picture.fileSize/1024).toFixed(2)+"kb\n\r图片长度:"+document.form1.show_picture.width+"\n\r图片高度:"+document.ry_jbxx_ActionForm.show_picture.height):alert(" 你的图片不符合规格");
}
function CaricaFoto(img)
{
  try{foto1= new Image();foto1.src=(img);Controlla(img);
  }catch(e){}
}

<html:form action="ry_jbxx_Action.do" styleId="ry_jbxxform" method="post" enctype="multipart/form-data">


  <TH>身份证号:</TH>
        <TD><html:text   styleId="ZFZid" styleClass="BOX" property="SFZ" maxlength="18" c_type="idcard" /> </TD>
以后增加以下代码
     <td rowspan="6" width="35%">
          <table width="100%">
            <tr>
              <td>
                <div align="center">
                  <A HREF="javascript:CaricaFoto(document.show_picture.src)" border="0">
                  <img name="show_picture"  border="1" src="<%com.chuangda.util.web.WriteHtmlElement.get_pic(request,out);%>"  width="150" height="180"  >
                  </a>
                  <br />
                  <input type="file" name="PIC" id="PIC" alt="照片上传" width="20" class="Button_B" onchange='javascript:pic_change()'/>
                </div>
              </td>
            </tr>
          </table>
        </td>

ry_jbxx_dao.java中
  public boolean update_ry_jbxx(Ry_jbxx_ActionForm thisForm)里
在Dbprocess.setdeleteData(t_dsql);后增加
      if(thisForm.getPIC().getFileSize()>0)
      {
         Dbprocess.insert_pic(thisForm.getPIC(),"RY_ID",thisForm.getRY_ID(),"T_RY_JBXX","picture");
      }

在public boolean insert_ry_jbxx(Ry_jbxx_ActionForm thisForm) 函数里
Dbprocess.setinsertData(sql);后增加
      if(thisForm.getPIC().getFileSize()>0)//czllfy 2007.3.29 add start
      {
        Dbprocess.insert_pic(thisForm.getPIC(),"RY_ID",thisForm.getRY_ID(),"T_RY_JBXX","picture");//czllfy 2007.3.29 新增加的一个函数
      }

Dbprocess.java里
重载insert_pic函数,
  public static boolean insert_pic(FormFile formfile,String indexname,String id,String tableName,String FiledName)
  {
      byte[] data = null;
      boolean result=true;
      int length = formfile.getFileSize();
      PreparedStatement ps = null;
      Statement st =null;
      ResultSet rs = null;
      Connection con = null;
      try {
          con = Dbconnection.getConnection();
          con.setAutoCommit(false);
          data = formfile.getFileData();
          st = con.createStatement();
          String sql="select "+FiledName+" from "+tableName+" where "+indexname+"='"+id+"' for update";
          rs=st.executeQuery(sql);
          if(rs.next()){
          oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
          if(blob==null){
            st.execute("update "+tableName+" set "+FiledName+"=empty_blob() where " +indexname+"='"+id+"'");
            rs=st.executeQuery(sql);
            if(rs.next()){
              blob = (oracle.sql.BLOB) rs.getBlob(1);
            }
          }
          if (blob != null) {
            BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
            out.write(data);
            out.flush();
           }
          con.commit();
          }else
              result=false;

    } catch (Exception e) {
      e.printStackTrace();
      result=false;
    }
    finally{
      Dbconnection.tryClose(con,st,rs);
    }
  return result;
  }

在WriteHtmlElement.java里修改函数get_pic()的内容为
public static void get_pic(HttpServletRequest request,JspWriter out ){
    String id="";
    ResultSet rs = null;
    String sql="";
    String rootpath=request.getContextPath().toString();
    int len=0;
    String ywb="";
    try{
        id=request.getParameter("ID");
        if(id==null || id.equals("")){
          id=request.getParameter("RY_ID");
          if(id==null||id.equals(""))
          {
            out.print("images/no_pic.gif");
            return;
          }
        }
    }catch(Exception  e){}
    ywb=(String)request.getAttribute("YW_TABLE");
    if(ywb.equals("NTLJJSY_JSZK"))
    {
         sql="select length(a.picture) as pic from ntljjsy_jszk a where a.id='"+id+"'";
    }
    else if(ywb.equals("T_RY_JBXX"))//czllfy 2007.2.29 add start
    {
       sql="select length(a.picture) as pic from t_ry_jbxx a where a.ry_id='"+id+"'";
    }//czllfy 2007.2.29 add end
    else
    {
        sql="select length(a.picture) as pic from ntljjsy_jszk a,"+ywb+" b where a.id=b.zid and b.id='"+id+"'";
    }
    try {
        rs=Dbprocess.getResult(sql);
        if(rs.next())
        {
          len = rs.getInt(1);
        }
        else
        {
            out.print("images/no_pic.gif");
            return;
        }
        if(len<=1)
            out.print("images/no_pic.gif");
        else{
            if(ywb.equals("NTLJJSY_JSZK"))
            {
                out.print(rootpath+"/picwebshow?id="+id+"&zb=ntljjsy_jszk&zd=picture");
            }
            else if(ywb.equals("T_RY_JBXX"))     //czllfy 2007.2.29 add start
            {
              out.print(rootpath+"/picwebshow?ry_id="+id+"&zb=t_ry_jbxx&zd=picture");
            }                                    //czllfy 2007.2.29 add end
            else
            {
                out.print(rootpath+"/picwebshow?id="+id+"&zb=ntljjsy_jszk&ywb="+request.getAttribute("YW_TABLE")+"&zd=picture");
            }
        }
    } catch (Exception e1) {
        try {
            out.print("images/no_pic.gif");
        } catch (IOException e2) {
        }
    }
}

PicWebShow.java里
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id = request.getParameter("id");
    if(id==null||id.equals("")) //czllfy 2007.2.29 add start
    {
      id=request.getParameter("ry_id");
    }                             //czllfy 2007.2.29 add end
    String zb = request.getParameter("zb");
    String ywb = request.getParameter("ywb");
    String zd = request.getParameter("zd");
    InputStream in = null;
    OutputStream os = null;
    try {
      if (id != null)
      {
        response.reset();
        response.setContentType(CONTENT_TYPE_JPEG);
        PicDAO _dao = new PicDAO();
        if(zb.equalsIgnoreCase("ntljjsy_jszk") && ywb==null)
        {
            in = _dao.getPic_jsz(id,zb,zd);
        }
        else if(zb.equalsIgnoreCase("T_RY_JBXX"))  //czllfy 2007.2.29 add start
        {
          in=_dao.getPic_ry(id,zb,zd);
        }                                          //czllfy 2007.2.29 add end
        else if(zb.equalsIgnoreCase("t_sb_cpxhb"))
        {
          in = _dao.getPic_jsz(id,zb,zd);
        }
        else
        {
            in = _dao.getPic(id,zb,ywb,zd);
        }

        os = response.getOutputStream();
        int len = 10 * 1024 * 1024;
        byte[] data = new byte[len];
        int length = 0;
        int i = 0;
        System.err.println("in is null!");
        if (in != null )
        {
          while ( (i = in.read(data)) != -1) {length++;}
          System.err.println("length = " + length);
          System.err.println("data = " + data.length);
          if(data != null )
          {
            os.write(data);
          }
        }
      }
    }
    catch (IOException ex) {}finally{
      try {
        if(in != null){in.close(); in = null;}
//        if(os != null){os.close(); os = null;}
      }
      catch (Exception ex) {
        }

    }
  }
picDAO.java里增加函数
  public InputStream getPic_ry(String id,String zb,String zd) {
    InputStream in = null;
    try {
     in =  _sql.getPic_ry(id,zb,zd);
    }
    catch (Exception e) {
      LogWriter.error("获取图片的流出错" + e.getMessage());
      return in;
    }
    finally {
      _sql = null;
    }
    return in;
  }
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics