首页 课程 师资 教程 报名

JSP图片的上传技显示

  • 2021-12-31 09:48:35
  • 886次 星辉

1.介绍

数据库应用,尤其是基于Web的应用,往往涉及图片信息的存储和显示。一般我们采用将要显示的图片存放在特定目录中,将对应图片的名称保存在数据库中,在JSP中建立对应的数据源,利用数据库访问技术对图片信息进行处理的方法。但是,如果我们想动态地显示图片,上述方法是不能满足需要的。我们必须将图片存储在数据库中,然后通过编程动态显示我们需要的图片。在实践中,可以使用JSP编程模式在数据库中存储和显示图像。

2.建立后台数据库

假设我们处理图片消息,那么我们就可以建立相应的数据库和数据表对象。我们要访问的数据表结构的SQL脚本如下:

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[picturenews]
GO
CREATE 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”。

3.将二进制图像存储到数据库

创建一个新的 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 object
String 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 Chinese
FileInputStream 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 database
out.println("Success,You Have Insert an Image Successfully");
%>

4.网页中的动态图片

接下来,我们需要编程从数据库中提取图像,代码如下。

<%@ 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 object
int id= Integer.parseInt(request.getParameter("id"));
//Get the number ID of the picture to be displayed and convert it to integer
String sql = "select image from picturenews WHERE";
//SQL statement to execute query
rs=stmt.executeQuery(sql);
while(rs.next()) {
ServletOutputStream sout = response.getOutputStream();
//Output stream of picture output
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//Output buffer input to page
in.read(b);
}
sout.flush();
//Input complete, clear buffer
sout.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>

以上web应用在Windows XP / SQL Server 2000 / Apache Tomcat 4.0/jbuilder环境下调试通过。如果大家想了解更相关知识,可以关注一下星辉的Java星辉在线学习,里面的课程内容详细,由浅到深,适合没有基础的小伙伴学习,希望对大家能够有所帮助。

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交