温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JSP图片怎么

发布时间:2022-09-26 11:00:06 来源:亿速云 阅读:85 作者:iii 栏目:开发技术

这篇文章主要介绍“JSP图片怎么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“JSP图片怎么”文章能帮助大家解决问题。

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]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”。

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 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");
%>

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 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图片怎么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jsp
AI