MySQL操作blob的經(jīng)驗(yàn)研討
以下的文章主要講述的是MySQL操作blob的經(jīng)驗(yàn)研討,如果你在MySQL操作blob的實(shí)際操作中有不解之處時(shí),你可以通過(guò)以下的文章對(duì)其的實(shí)際應(yīng)用與功能有所了解,下面是文章的具體介紹,望你瀏覽完以下的內(nèi)容會(huì)有所收獲。
jsp(SUN企業(yè)級(jí)應(yīng)用的首選)+MySQL(和PHP搭配之最佳組合) 記住 要用MySQL(和PHP搭配之最佳組合)的longblob類型來(lái)存默認(rèn)的MySQL操作blob大小不夠
數(shù)據(jù)庫(kù)字段:id?。ǎ悖瑁幔颍。穑椋恪。╨ongblob)
轉(zhuǎn)載請(qǐng)注明出處,這時(shí)我與我的知己的合作的結(jié)過(guò)
原來(lái)操作blob字段時(shí)都要先差個(gè)空值,在查blob,好麻煩,用prepareStatment就不用那么麻煩了,哈哈
postblob.heml頁(yè)面
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xml(標(biāo)準(zhǔn)化越來(lái)越近了)ns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>無(wú)標(biāo)題文檔</title>
- </head>
- <body>
- <form action="testblob.jsp(SUN企業(yè)級(jí)應(yīng)用的首選)" 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>file</td>
- <td><input name="file" type="file" /></td>
- </tr>
- <tr>
- <td><input type="submit" value="提交"/></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- testblob.jsp(SUN企業(yè)級(jí)應(yīng)用的首選)
- <%@ page contentType="text/html;charset=gb2312"%>
- <%@ page import="java.sql.*" %>
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%@ page import="java.io.*"%>
- <html xml(標(biāo)準(zhǔn)化越來(lái)越近了)ns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>無(wú)標(biāo)題文檔</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企業(yè)級(jí)應(yīng)用的首選)">查看圖片</a>
- <a href="postblob.html">返回</a>
- </body>
- </html>
- readblob.jsp(SUN企業(yè)級(jí)應(yīng)用的首選)
- <%@ page contentType="text/html;charset=gb2312"%>
- <%@ page import="java.sql.*, javax.sql.*" %>
- <%@ page import="java.util.*"%>
- <%@ page import="java.text.*"%>
- <%@ page import="java.io.*"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xml(標(biāo)準(zhǔn)化越來(lái)越近了)ns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>無(wú)標(biāo)題文檔</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);時(shí),該方法把inputstream中的內(nèi)容在一個(gè)新的頁(yè)面中輸出,
如果本頁(yè)中還有別的內(nèi)容要輸出的話,只有把上述方法改為,bytesRead = in.read(by)) ;
再用out.print(new String(by));方法輸出結(jié)果,注意在這里不能用by.toString()方法,該方法返回的是要輸出內(nèi)容的內(nèi)存地址。MySQL(和PHP搭配之最佳組合)中有MySQL操作blob textarea類型大小了66536基本上放點(diǎn)小的東東就足夠了,哈哈,但是現(xiàn)在的數(shù)碼pic越來(lái)越大就只能用longblob了。
【編輯推薦】