
已解决问题
谷歌shao78cs用户在2016.05.08提交了关于“凯迪拉克elr用JSP实现**库图片的存储与显示实例”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-14T16:42:18。希望大家能够帮助她。详细问题描述及疑问:期待您的答案,你就是当代的活雷锋,太感谢了 !
详细问题描述及疑问:期待您的答案,你就是当代的活雷锋,太感谢了 !
1.引言
**库应用程序,特别是基于WEB的**库应用程序,常会涉及到图片信息的存储和显示。
通常
2.建立后台**库
i
whereid=object_id(N'[dbo].[p]')andOBJECTPROPERTY(id,N
dr
GO
CREATETABLE[dbo]
[picid][int]IDENTITY(1,1)
[p
[pic][image]**LL
)ON[PRIMARY
GO
3.向**库存储二进制图片
启动Dream
<%@pagecontentType="text/ht
<%
Stringpa
StringbasePath=request.
+":"+request.getS
%>
<!
<html>
<hea
<basehre
<title>MyJSP'I
<metahttp-equiv
<met
<metahttp-equiv="expires"cont
<metahttp-equiv="keywords"content="keyword1,key
<metahttp-eq
<!--
<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>