展开全部
有两种方法一是用上传的组建jspSmartUpload的Request,
还有一种不用组建,但在e69da5e6ba9062616964757a686964616f31333238653233form表单中不能加入ENCTYPE= "multipart/form-data "
我给你的案例吧
建立后台数据库
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[p]
GO
CREATE TABLE [dbo].[p] (
[picid] [int] IDENTITY (1, 1) NOT NULL ,
[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
3.向数据库存储二进制图片
启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>
My JSP 'InputImage.jsp' starting page题目
图片
将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片数据存入数据库的,具体代码如下所示:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
My JSP 'testimage.jsp' starting pagerequest.setCharacterEncoding("gb2312");
//建立Statement对象
String picname=request.getParameter("picname");
String pic=request.getParameter("pic");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
FileInputStream str=new FileInputStream(pic);
String sql="insert into p(picname,pic) values(?,?)";
PreparedStatement pstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//将数据存入数据库
out.println("Success,You Have Insert an Image Successfully");
%>
4. 网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
My JSP 'testimageout.jsp' starting pageint id= Integer.parseInt(request.getParameter("picid"));
String sql = "select pic from p WHERE picid="+id;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
//图片输出的输出流
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//将缓冲区的输入输出到页面
in.read(b);
}
sout.flush();
//输入完毕,清除缓冲
sout.close();
}
%>
将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+
":"+request.getServerPort()+path+"/";
%>
My JSP 'lookpic.jsp' starting pageString sql = "select * from p";
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
%>
" width="100" height="100">
}
rs.close();
%>
已赞过
已踩过<
你对这个回答的评价是?
评论
收起