熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

用JSP實現數據庫圖片的存儲與顯示實例[2]

2013-11-15 12:18:50  來源: JSP教程 

  將此文件保存為InputImagejsp文件其中testimagejsp文件是用來將圖片數據存入數據庫的具體代碼如下所示

<%@ page contentType=text/html;charset=gb%>
<%@ page import=javasql* %>
<%@ page import=javautil*%>
<%@ page import=javatext*%>
<%@ page import=javaio*%>
<jsp:useBean id=conn scope=page class=dbconnDBResult/>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+://+requestgetServerName()+
:+requestgetServerPort()+path+/;
%>
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
<html>
 <head>
  <base href=<%=basePath%>>
  <title>My JSP testimagejsp starting page</title>
    <meta httpequiv=pragma content=nocache>
    <meta httpequiv=cachecontrol content=nocache>
    <meta httpequiv=expires content=>  
    <meta httpequiv=keywords content=keywordkeywordkeyword>
    <meta httpequiv=description content=This is my page>
    <!
    <link rel=stylesheet type=text/css href=stylescss>
    >
 </head>
<body>
<%
   requestsetCharacterEncoding(gb);
//建立Statement對象
String picname=requestgetParameter(picname);
String pic=requestgetParameter(pic);
//獲得所要顯示圖片的標題存儲路徑內容並進行中文編碼
FileInputStream str=new FileInputStream(pic);
String sql=insert into p(picnamepic) values(??);
PreparedStatement pstmt=conngetPreparedStatement(sql);
pstmtsetString(picname);
pstmtsetBinaryStream(strstravailable());
pstmtexecute();
//將數據存入數據庫
outprintln(SuccessYou Have Insert an Image Successfully);
%>
</body>
</html>


   網頁中動態顯示圖片

  接下來我們要編程從數據庫中取出圖片其代碼如下所示

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20706.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.