以下的文章主要讲述的是mysql操作blob的经验研讨,如果你在mysql操作blob的实际操作中有不解之处时,你可以通过以下的文章对其的实际应用与功能有所了解,下面是文章的具体介绍,望你浏览完以下的内容会有所收获。 jsp(sun企业级应用的首选)+mysql(和php搭配
以下的文章主要讲述的是mysql操作blob的经验研讨,如果你在mysql操作blob的实际操作中有不解之处时,你可以通过以下的文章对其的实际应用与功能有所了解,下面是文章的具体介绍,望你浏览完以下的内容会有所收获。
jsp(sun企业级应用的首选)+mysql(和php搭配之最佳组合) 记住 要用mysql(和php搭配之最佳组合)的longblob类型来存默认的mysql操作blob大小不够
数据库字段:id (char) pic (longblob)
转载请注明出处,这时我与我的知己的合作的结过
原来操作blob字段时都要先差个空值,在查blob,好麻烦,用preparestatment就不用那么麻烦了,哈哈
postblob.heml页面
nbsp;html public -//w3c//dtd xhtml 1.0 transitional//en http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd> html xml(标准化越来越近了)ns=http://www.w3.org/1999/xhtml> head> meta http-equiv=content-type content=text/html; charset=gb2312 /> title>无标题文档title> head> body> form action=testblob.jsp(sun企业级应用的首选) method=post > table width=291 border=1> tr> td width=107>id td> td width=168>input name=id type=text />td> tr> tr> td>filetd> td>input name=file type=file />td> tr> tr> td>input type=submit value=提交/>td> tr> table> form> body> html> testblob.jsp(sun企业级应用的首选) %@ page contenttype=text/html;charset=gb2312%> %@ page import=java.sql.* %> %@ page import=java.util.*%> %@ page import=java.text.*%> %@ page import=java.io.*%> html xml(标准化越来越近了)ns=http://www.w3.org/1999/xhtml> head> meta http-equiv=content-type content=text/html; charset=gb2312 /> title>无标题文档title> head> body> % 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(org.gjt.mm.mysql(和php搭配之最佳组合).driver).newinstance(); conn= java.sql.drivermanager.getconnection(jdbc:mysql(和php搭配之最佳组合)://localhost/test,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(); %> a href=readblob.jsp(sun企业级应用的首选)>查看图片a> a href=postblob.html>返回a> body> html> readblob.jsp(sun企业级应用的首选) %@ page contenttype=text/html;charset=gb2312%> %@ page import=java.sql.*, javax.sql.* %> %@ page import=java.util.*%> %@ page import=java.text.*%> %@ page import=java.io.*%> nbsp;html public -//w3c//dtd xhtml 1.0 transitional//en http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd> html xml(标准化越来越近了)ns=http://www.w3.org/1999/xhtml> head> meta http-equiv=content-type content=text/html; charset=gb2312 /> title>无标题文档title> head> body> % java.sql.connection conn; resultset rs=null; class.forname(org.gjt.mm.mysql(和php搭配之最佳组合).driver).newinstance(); conn= java.sql.drivermanager.getconnection(jdbc:mysql(和php搭配之最佳组合)://localhost/test,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(); } %> body> html>
注意:在用sos.write(by, 0, bytesread);时,该方法把inputstream中的内容在一个新的页面中输出,
如果本页中还有别的内容要输出的话,只有把上述方法改为,bytesread = in.read(by)) ;
再用out.print(new string(by));方法输出结果,注意在这里不能用by.tostring()方法,该方法返回的是要输出内容的内存地址。mysql(和php搭配之最佳组合)中有mysql操作blob textarea类型大小了66536基本上放点小的东东就足够了,哈哈,但是现在的数码pic越来越大就只能用longblob了。