1. Introduction
Database applications, especially WEB-based database applications, usually involve the storage and display of image information.
The common method is to store the image to be displayed in a specific directory, store the name of the image in the database, and create the corresponding data source in JSP, use Database Access technology to process image information. However, if we want to dynamically display images, the above method cannot meet our needs. We must store images in the database and dynamically display the images we need through programming. In practice, the JSP programming mode can be used to store and display image databases.
2. Create a background database
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[p] GO CREATE TABLE [dbo].[p] ( [picid] [int] IDENTITY (1, 1) NOT NULL , [picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [pic] [image] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO |
3. store binary images to the database
Start Dreamweaver MX and create a new JSP file. The Code is as follows.
<% @ Page ContentType = "text/html; charset = gb2312" %> <% String path = Request. getContextPath (); String basePath = Request. getScheme () + ": //" + request. getServerName () + ":" + Request. getServerPort () + path + "/"; %> <! DOCTYPE Html public "-// W3C // dtd html 4.01 Transitional // EN "> <Html> <Head> <Base Href = "<% = basePath %>"> <Title> My JSP 'inputimage. jsp' Starting page </title> <Meta http-equiv = "pragma" Content = "no-cache"> <Meta http-equiv = "cache-control" Content = "no-cache"> <Meta http-equiv = "expires" Content = "0"> <Meta http-equiv = "keywords" Content = "keyword1, keyword2, keyword3"> <Meta Http-equiv = "description" content = "This is my page"> <! -- <Link rel = "stylesheet" type = "text/css" href = "styles.css"> --> </Head> <Body> <Form action = "testimage. jsp" Method = "Post"> <br> Question <input name = "picname" Type = "text"> <br> Image <input name = "pic" Type = "file"> <br> <Input type = "Submit" name = "button1" Value = "Submit"> <br> </Form> </Body> </Html> |
Save this file as an InputImage. jsp file. The testimage. jsp file is used to store image data to the database. The specific code is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ Page Import = "Java. SQL. *" %> <% @ Page import = "Java. util. *" %> <% @ Page import = "Java. Text. *" %> <% @ Page Import = "Java. Io. *" %> <JSP: usebean id = "conn" Scope = "page" Class = "dbconn. dbresult"/> <% String Path = Request. getcontextpath (); String basepath = Request. getscheme () + ": //" + request. getservername () + ":" + Request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 Transitional // en "> <HTML> <Head> <Base Href = "<% = basepath %>"> <Title> my JSP 'testimage. jsp 'starting Page </title> <Meta http-equiv = "Pragma" Content = "no-Cache"> <Meta http-equiv = "cache-control" Content = "no-Cache"> <Meta http-equiv = "expires" Content = "0"> <Meta http-equiv = "keywords" Content = "keyword1, keyword2, keyword3"> <Meta HTTP-equiv = "Description" content = "this is my page"> <! -- <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"> --> </Head> <Body> <% Request. setcharacterencoding ("gb2312 "); // Create a statement object String Picname = request. getparameter ("picname "); String PIC = request. getparameter ("pic "); // Obtain the title, storage path, and content of the image to be displayed, and encode it in Chinese. Fileinputstream STR = new fileinputstream (PIC ); String SQL = "insert into P (picname, PIC) Values (?,?) "; Preparedstatement Pstmt = conn. getpreparedstatement (SQL ); Pstmt. setstring (1, picname ); Pstmt. setbinarystream (2, STR, str. Available ()); Pstmt.exe cute (); // Store data into the database Out. println ("Success, You Have Insert an Image Successfully "); %> </Body> </Html> |
4. dynamically display images on the webpage
Next, we will program to retrieve images from the database. The Code is as follows.
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ Page Import = "java. SQL. *" %> <% @ Page import = "java. util. *" %> <% @ Page import = "java. text. *" %> <% @ Page Import = "java. io. *" %> <Jsp: useBean id = "conn" scope = "page" Class = "dbconn. DBResult"/> <% String path = Request. getContextPath (); String basePath = Request. getScheme () + ": //" + request. getServerName () + ":" + Request. getServerPort () + path + "/"; %> <! DOCTYPE Html public "-// W3C // dtd html 4.01 Transitional // EN "> <Html> <Head> <Base Href = "<% = basePath %>"> <Title> My JSP 'testimageout. jput' Starting page </title> <Meta http-equiv = "pragma" Content = "no-cache"> <Meta http-equiv = "cache-control" Content = "no-cache"> <Meta http-equiv = "expires" Content = "0"> <Meta http-equiv = "keywords" Content = "keyword1, keyword2, keyword3"> <Meta Http-equiv = "description" content = "This is my page"> <! -- <Link rel = "stylesheet" type = "text/css" href = "styles.css"> --> </Head> <Body> <% Int id = Integer. parseint (request. getparameter ("picid ")); String SQL = "select PIC From P where picid = "+ ID; Resultset rs = conn. getresult (SQL ); While (Rs. Next ()) { Servletoutputstream sout = Response. getoutputstream (); // Output stream of image output Inputstream in = Rs. getbinarystream (1 ); Byte B [] = new byte [0x7a120]; For (int I = in. Read (B); I! = -1 ;) { Sout. Write (B ); // Output the buffer input to the page In. Read (B ); } Sout. Flush (); // After the input is complete, clear the buffer. Sout. Close (); } %> </Body> </Html> |
Save this file as a testimageout. jsp file. The next step is to use HTML tags:
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+ ":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <base href="<%=basePath%>"> <title>My JSP 'lookpic.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <body> <% String sql = "select * from p"; ResultSet rs=conn.getResult(sql); while(rs.next()) { %> <ccid_file values="testimageout" % />" width="100" height="100"> <br> <% } rs.close(); %> </body>
|