圖片上傳到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" οnchange='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;
}
發佈了12 篇原創文章 · 獲贊 1 · 訪問量 2225
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章