用JSP实现**库图片的存储与显示实例

时间:2016.05.08 发布人:shao78cs

用JSP实现**库图片的存储与显示实例

已解决问题

谷歌shao78cs用户在2016.05.08提交了关于“凯迪拉克elr用JSP实现**库图片的存储与显示实例”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-14T16:42:18。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,你就是当代的活雷锋,太感谢了 !

希望以下的回答,能够帮助你。

第1个回答

用户名:z479976127  

1.引言

  **库应用程序,特别是基于WEB的**库应用程序,常会涉及到图片信息的存储和显示。

  通常我们使用的方法是将所要显示的图片存在特定的目录下,在**库中保存相应的图片的名称,在JSP中建立相应的**源,利用**库访问技术处理图片信息。但是问答,如果我们想动态好袁齐卫广虽企候的显示图片,上述方法就不能满足需要了。我们必须把局在图片存入**库,然后通过编紧及程动态地显示我们需要的图片。实际操作中,可以利用JSP的编程模式来实现图片的**库存储和显示。

  2.建立后台**库

i八fexists(select*fromdbo.sysobjects
whereid=object_id(N'[dbo].[p]')andOBJECTPROPERTY(id,N斯理伟校'IsUserTable')=1)
droptable[dbo].[p]
GO
CREATETABLE[dbo].[p](
[picid][int]IDENTITY(1,1)台岩从治龙NOT**LL,
[picname][varchar](50)COLLATEChinese_PRC_CI_AS阶督具专师顺鲜源孩员**LL,
[pic][image]**LL
)ON[PRIMARY]TEXTIMAGE_ON[PRIMARY]
GO

  3.向**库存储二进制图片

  启动Dream项背入技历资安今话边极we**erMX后,新建一个JSP文件。其代码如下所示。

<%@pagecontentType="text/html;charset=gb2312"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()
+":"+request.getS师尔又验哪今erverPort()+path+"/";
%>
<!下水虽便太DOCTYPEh**TMLPUBLIC"-//W3C//DTDh**TML4.01Transitional//EN">
<html>
<hea背距排团误容台d>
<basehre缺烧测医移往误露议笔f="<%=basePath%>">
<title>MyJSP'InputImage.jsp'startingpa怀缩ge</title>
<metahttp-equiv="pragma"content="no-cache">
<met滑难ahttp-equiv="cache-control"content="no-c声ache">
<metahttp-equiv="expires"content="0">  
<metahttp-equiv="keywords"content="keyword1,key脱红所财鲁声期设视word2,keyword3">
<metahttp-eq出吸输兰以地蒸盟uiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<formaction="testimage.jsp"method="POST"><br>
题目<inputname="picname"type="text"><br>
图片<inputname="pic"type="file"><br>
<inputtype="Submit"name="button1"value="提交"><br>
</form>
</body>
</html>

  将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片**存入**库的,具体代码如下所示:

<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="j**a.sql.*"%>
<%@pageimport="j**a.util.*"%>
<%@pageimport="j**a.text.*"%>
<%@pageimport="***.io.*"%>
<jsp:useBeanid="conn"scope="page"class="dbconn.DBResult"/>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEh**TMLPUBLIC"-//W3C//DTDh**TML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'testimage.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">  
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
//建立Statement对象
Stringpicname=request.getParameter("picname");
Stringpic=request.getParameter("pic");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
FileInputStreamstr=newFileInputStream(pic);
Stringsql="insertintop(picname,pic)values(?,?)";
PreparedStatementpstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.**ailable());
pstmt.execute();
//将**存入**库
out.println("Success,Youh****eInsertanImageSuccessfully");
%>
</body>
</html>

  4.网页中动态显示图片

  接下来我们要编程从**库中取出图片,其代码如下所示。

<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="j**a.sql.*"%>
<%@pageimport="j**a.util.*"%>
<%@pageimport="j**a.text.*"%>
<%@pageimport="***.io.*"%>
<jsp:useBeanid="conn"scope="page"class="dbconn.DBResult"/>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEh**TMLPUBLIC"-//W3C//DTDh**TML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'testimageout.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">  
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<%
intid=Integer.parseInt(request.getParameter("picid"));
Stringsql="selectpicfrompWh**EREpicid="+id;
ResultSetrs=conn.getResult(sql);
while(***.next())
{
ServletOutputStreamsout=response.getOutputStream();
//图片输出的输出流
InputStreamin=rs.getBinaryStream(1);
byteb[]=newbyte[0x7a120];
for(inti=***.read(b);i!=-1;)
{
sout.write(b);
//将缓冲区的输入输出到页面
***.read(b);
}
sout.flush();
//输入完毕,**缓冲
sout.close();
}
%>
</body>
</html>

  将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用h**TML标记:

<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="j**a.sql.*"%>
<%@pageimport="j**a.util.*"%>
<%@pageimport="j**a.text.*"%>
<%@pageimport="***.io.*"%>
<jsp:useBeanid="conn"scope="page"class="dbconn.DBResult"/>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEh**TMLPUBLIC"-//W3C//DTDh**TML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'lookpic.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">  
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<%
Stringsql="select*fromp";
ResultSetrs=conn.getResult(sql);
while(***.next())
{
%>
<ccid_filevalues="testimageout"%/>"width="100"height="100">
<br>
<%
}
rs.close();
%>
</body>
</html>