您好,登錄后才能下訂單哦!
這篇文章主要介紹“JSP圖片怎么”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“JSP圖片怎么”文章能幫助大家解決問題。
數據庫應用,尤其是基于Web的應用,往往涉及圖片信息的存儲和顯示。一般我們采用將要顯示的圖片存放在特定目錄中,將對應圖片的名稱保存在數據庫中,在JSP中建立對應的數據源,利用數據庫訪問技術對圖片信息進行處理的方法。但是,如果我們想動態地顯示圖片,上述方法是不能滿足需要的。我們必須將圖片存儲在數據庫中,然后通過編程動態顯示我們需要的圖片。在實踐中,可以使用JSP編程模式在數據庫中存儲和顯示圖像。
假設我們處理圖片消息,那么我們就可以建立相應的數據庫和數據表對象。我們要訪問的數據表結構的SQL腳本如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[picturenews]GOCREATE TABLE [dbo].[picturenews] ( [id] [int] IDENTITY (1, 1) NOT NULL , [image] [image] NULL , [content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL , [detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO
表格圖片中以字段ID作為標識,每存儲一行數據自動加1。Field image
用于存儲圖片信息,數據類型為“image”。
創建一個新的 JSP 文件。代碼如下。
<%@ page contentType="text/html;charset=gb2312"%><HTML><HEAD>< title > store pictures < / Title ></HEAD><body><! -- the form below will pass data to testimage.jsp file in post method -- ><FORM METHOD=POST ACTION="testimage.jsp">News Title: < input type = "text" name = "content" > < br >News picture: < input type = "file" name = "image" > < br >EA > < br ><INPUT TYPE="submit"></form></body></HTML>
將此文件保存為inputimage.jsp文件,其中testimage.jsp文件用于將圖像數據存儲到數據庫中。具體代碼如下:
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <html> <body> <% class. Forname ("com. Microsoft. JDBC. Sqlserver. Sqlserverdriver"); // load driver class Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");//Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.Statement stmt=con.createStatement();//Create statement objectString content=request.getParameter("content"); content=new String(content.getBytes("8859_1"),"gb2312"); String filename=request.getParameter("image"); filename=new String(filename.getBytes("8859_1"),"gb2312"); String detail=request.getParameter("txtmail"); detail=new String(detail.getBytes("8859_1"),"gb2312");//Get the title, storage path and content of the picture to be displayed, and code it in ChineseFileInputStream str=new FileInputStream(filename); String sql="insert into picturenews(content,image,detail) values(?,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1,content); pstmt.setBinaryStream(2,str,str.available()); pstmt.setString(3,detail); pstmt.execute();//Store data in databaseout.println("Success,You Have Insert an Image Successfully"); %>
接下來,我們需要編程從數據庫中提取圖像,代碼如下。
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <html> <body> <% % Class.forName (" com.microsoft.jdbc.sqlserver.SQLServerDriver "); // load driver class Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");//Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.Statement stmt=con.createStatement(); ResultSet rs=null;//Create a resultset objectint id= Integer.parseInt(request.getParameter("id"));//Get the number ID of the picture to be displayed and convert it to integerString sql = "select image from picturenews WHERE";//SQL statement to execute queryrs=stmt.executeQuery(sql);while(rs.next()) { ServletOutputStream sout = response.getOutputStream();//Output stream of picture outputInputStream in = rs.getBinaryStream(1);byte b[] = new byte[0x7a120];for(int i = in.read(b); i != -1;) { sout.write(b);//Output buffer input to pagein.read(b); } sout.flush();//Input complete, clear buffersout.close(); } %> </body> </html>
將此文件另存為 testimageout.jsp 文件。下一步是使用 HTML 標簽:
<IMG src=”testimageout.jsp?id=<%=rs.getInt(“id”)%>” width=100 height=100>
取出要顯示的圖片,其中id為要顯示的圖片編號帶走。在這個例子中,我們輸出了第一張和最后一張圖片信息。詳細的程序代碼如下所示。
<%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.sql.*" %><html><head>< title > dynamically display database picture < / Title ></head><body><%% Class.forName (" com.microsoft.jdbc.sqlserver.SQLServerDriver "); // load driver class Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa"); //Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.Statement stmt=con.createStatement();String sql=new String();sql= "select * from picturenews";ResultSet rs=stmt.executeQuery(sql);rs.last(); //Move pointer to last record%> <table><tr><td><IMG height=99 src="testimageout.jsp?id=1" width=136></td>//Take out the first picture<td><IMG height=99 src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=136></td>//Take out the last picture</tr></table></body></html>
關于“JSP圖片怎么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。