java代码
一、数据库端操作: 1 在mysql下建一个数据库名字叫 testpic ===> mysql>create database testpic; 2 在testpic库下建一数据表test,只有两字段 ===> mysql>use testpic; ===> mysql>create table test (id int, pic blob); 一、数据库端操作:1 在mysql下建一个数据库名字叫 testpic ===> mysql>create database testpic;2 在testpic库下建一数据表test,只有两字段 ===> mysql>use testpic; ===> mysql>create table test (id int, pic blob);
二、相关的html jsp文件
**********************************************************************************************
登录界面 postblob.html
java代码 nbsp;html public -//w3c//dtd html 4.01 transitional//en http://www.w3.org/tr/html4/loose.dtd> insert title here id
file
insert title hereid
file
**********************************************************************************************
readblob.jsp界面源码
java代码 pageencoding=utf-8%> nbsp;html public -//w3c//dtd html 4.01 transitional//en http://www.w3.org/tr/html4/loose.dtd> insert title here java.sql.connection conn; resultset rs=null; class.forname(com.mysql.jdbc.driver).newinstance(); conn= java.sql.drivermanager.getconnection(jdbc:mysql://localhost/testpic,root,root); statement stmt=conn.createstatement(); rs=stmt.executequery(select * from test where id=1); if(rs.next()) { blob b = rs.getblob(pic); int size =(int)b.length(); out.print(size); inputstream in=b.getbinarystream(); byte[] by= new byte[size]; response.setcontenttype(image/jpeg); servletoutputstream sos = response.getoutputstream(); int bytesread = 0; while ((bytesread = in.read(by)) != -1) { sos.write(by, 0, bytesread); } in.close(); sos.flush(); } %> insert title here
**********************************************************************************************
testblob.jsp界面源码
java代码 pageencoding=utf-8%> nbsp;html public -//w3c//dtd html 4.01 transitional//en http://www.w3.org/tr/html4/loose.dtd> insert title here string id=request.getparameter(id); string file=request.getparameter(file); out.print(id); out.print(file); fileinputstream str=new fileinputstream(file); out.print(str.available()); java.sql.connection conn; java.lang.string strconn; class.forname(com.mysql.jdbc.driver).newinstance(); conn= java.sql.drivermanager.getconnection(jdbc:mysql://localhost/testpic,root,root); string sql=insert into test(id,pic) values(?,?); preparedstatement pstmt=conn.preparestatement(sql); pstmt.setstring(1,id); pstmt.setbinarystream(2,str,str.available()); pstmt.execute(); out.println(success,you have insert an image successfully); pstmt.close(); %> 查看图片 返回