html5 java 图片上传_java实现图片上传至服务器并显示,如何做?希望要具体的代码实现...
展開全部
有兩種方法一是用上傳的組建jspSmartUpload的Request,
還有一種不用組建,但在e69da5e6ba9062616964757a686964616f31333238653233form表單中不能加入ENCTYPE= "multipart/form-data "
我給你的案例吧
建立后臺數(shù)據(jù)庫
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.向數(shù)據(jù)庫存儲二進(jìn)制圖片
啟動Dreamweaver MX后,新建一個(gè)JSP文件。其代碼如下所示。
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>
My JSP 'InputImage.jsp' starting page題目
圖片
將此文件保存為InputImage.jsp文件,其中testimage.jsp文件是用來將圖片數(shù)據(jù)存入數(shù)據(jù)庫的,具體代碼如下所示:
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");
//獲得所要顯示圖片的標(biāo)題、存儲路徑、內(nèi)容,并進(jìn)行中文編碼
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();
//將數(shù)據(jù)存入數(shù)據(jù)庫
out.println("Success,You Have Insert an Image Successfully");
%>
4. 網(wǎng)頁中動態(tài)顯示圖片
接下來我們要編程從數(shù)據(jù)庫中取出圖片,其代碼如下所示。
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);
//將緩沖區(qū)的輸入輸出到頁面
in.read(b);
}
sout.flush();
//輸入完畢,清除緩沖
sout.close();
}
%>
將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標(biāo)記:
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();
%>
已贊過
已踩過<
你對這個(gè)回答的評價(jià)是?
評論
收起
總結(jié)
以上是生活随笔為你收集整理的html5 java 图片上传_java实现图片上传至服务器并显示,如何做?希望要具体的代码实现...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS7 源码编译安装Redis
- 下一篇: 如何解决在使用ElementUI时发现有