Home  >  Article  >  Database  >  MySQL操作blob的经验研讨

MySQL操作blob的经验研讨

WBOY
WBOYOriginal
2016-06-07 16:11:341054browse

以下的文章主要讲述的是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页面

<ol class="dp-xml">
<li class="alt"><span><span>nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">html</span><span> xml(标准化越来越近了)</span><span class="attribute">ns</span><span>=</span><span class="attribute-value">"http://www.w3.org/1999/xhtml"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>无标题文档</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li>
<li>
<span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li class="alt"><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">form</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"testblob.jsp(SUN企业级应用的首选)"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"291"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"1"</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"107"</span><span class="tag">></span><span>id </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"168"</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"id"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="tag">/></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span>
</li>
<li><span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">td</span><span class="tag">></span><span>file</span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">td</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"file"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"file"</span><span> </span><span class="tag">/></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span>
</li>
<li><span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">td</span><span class="tag">></span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"提交"</span><span class="tag">/></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li>
<span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
<li class="alt"><span>testblob.jsp(SUN企业级应用的首选)  </span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">contentType</span><span>=</span><span class="attribute-value">"text/html;charset=gb2312"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.sql.*"</span><span> %</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span>%</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.text.*"</span><span>%</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.io.*"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">html</span><span> xml(标准化越来越近了)</span><span class="attribute">ns</span><span>=</span><span class="attribute-value">"http://www.w3.org/1999/xhtml"</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>无标题文档</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span>%   </span></span></li>
<li>
<span>String </span><span class="attribute">id</span><span>=</span><span class="attribute-value">request</span><span>.getParameter("id");  </span>
</li>
<li class="alt">
<span>String </span><span class="attribute">file</span><span>=</span><span class="attribute-value">request</span><span>.getParameter("file");  </span>
</li>
<li><span>out.print(id);  </span></li>
<li class="alt"><span>out.print(file);  </span></li>
<li>
<span>FileInputStream </span><span class="attribute">str</span><span>=</span><span class="attribute-value">new</span><span> FileInputStream(file);  </span>
</li>
<li class="alt"><span>out.print(str.available());  </span></li>
<li><span>java.sql.Connection conn;   </span></li>
<li class="alt"><span>java.lang.String strConn;   </span></li>
<li>
<span>Class.forName("org.gjt.mm.</span>MySQL<span>(和PHP搭配之最佳组合).Driver").newInstance();   </span>
</li>
<li class="alt">
<span class="attribute">conn</span><span>= </span><span class="attribute-value">java</span><span>.sql.DriverManager.getConnection("jdbc:</span>MySQL<span>(和PHP搭配之最佳组合)://localhost/test","root","");   </span>
</li>
<li>
<span>String </span><span class="attribute">sql</span><span>=</span><span class="attribute-value">"insert into test(id,pic) values(?,?)"</span><span>;   </span>
</li>
<li class="alt">
<span>PreparedStatement </span><span class="attribute">pstmt</span><span>=</span><span class="attribute-value">conn</span><span>.prepareStatement(sql);   </span>
</li>
<li><span>pstmt.setString(1,id);  </span></li>
<li class="alt"><span>pstmt.setBinaryStream(2,str,str.available());   </span></li>
<li><span>pstmt.execute();   </span></li>
<li class="alt"><span>out.println("Success,You Have Insert an Image Successfully");  </span></li>
<li><span>pstmt.close();  </span></li>
<li class="alt">
<span>%</span><span class="tag">></span><span>   </span>
</li>
<li><span class="tag"><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"readblob.jsp(SUN企业级应用的首选)"</span><span class="tag">></span><span>查看图片</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"postblob.html"</span><span class="tag">></span><span>返回</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span> </span></span></li>
<li>
<span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
<li><span>readblob.jsp(SUN企业级应用的首选)  </span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">contentType</span><span>=</span><span class="attribute-value">"text/html;charset=gb2312"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.sql.*, javax.sql.*"</span><span> %</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span>%</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.text.*"</span><span>%</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span>%@ page </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.io.*"</span><span>%</span><span class="tag">></span><span>   </span></span></li>
<li>
<span>nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span class="tag">></span><span> </span>
</li>
<li class="alt"><span class="tag"><span class="tag-name">html</span><span> xml(标准化越来越近了)</span><span class="attribute">ns</span><span>=</span><span class="attribute-value">"http://www.w3.org/1999/xhtml"</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>无标题文档</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span>%  </span></span></li>
<li><span>java.sql.Connection conn;  </span></li>
<li class="alt">
<span>ResultSet </span><span class="attribute">rs</span><span>=</span><span class="attribute-value">null</span><span>;  </span>
</li>
<li>
<span>Class.forName("org.gjt.mm.</span>MySQL<span>(和PHP搭配之最佳组合).Driver").newInstance();   </span>
</li>
<li class="alt">
<span class="attribute">conn</span><span>= </span><span class="attribute-value">java</span><span>.sql.DriverManager.getConnection("jdbc:</span>MySQL<span>(和PHP搭配之最佳组合)://localhost/test","root","");   </span>
</li>
<li>
<span>Statement </span><span class="attribute">stmt</span><span>=</span><span class="attribute-value">conn</span><span>.createStatement();   </span>
</li>
<li class="alt">
<span class="attribute">rs</span><span>=</span><span class="attribute-value">stmt</span><span>.executeQuery("select * from test where </span><span class="attribute">id</span><span>=</span><span class="attribute-value">'1'</span><span>");  </span>
</li>
<li><span>if(rs.next())  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>Blob </span><span class="attribute">b</span><span> = </span><span class="attribute-value">rs</span><span>.getBlob("pic");  </span>
</li>
<li class="alt">
<span>int </span><span class="attribute">size</span><span> =(int)b.length();  </span>
</li>
<li><span>out.print(size);  </span></li>
<li class="alt">
<span>InputStream </span><span class="attribute">in</span><span>=</span><span class="attribute-value">b</span><span>.getBinaryStream();  </span>
</li>
<li>
<span>byte[] </span><span class="attribute">by</span><span>= </span><span class="attribute-value">new</span><span> byte[size];  </span>
</li>
<li class="alt"><span>response.setContentType("image/jpeg");   </span></li>
<li>
<span>ServletOutputStream </span><span class="attribute">sos</span><span> = </span><span class="attribute-value">response</span><span>.getOutputStream();  </span>
</li>
<li class="alt">
<span>int </span><span class="attribute">bytesRead</span><span> = </span><span class="attribute-value">0</span><span>;  </span>
</li>
<li>
<span>while ((</span><span class="attribute">bytesRead</span><span> = </span><span class="attribute-value">in</span><span>.read(by)) != -1) {  </span>
</li>
<li class="alt"><span>sos.write(by, 0, bytesRead);  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>in.close();  </span></li>
<li><span>sos.flush();  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>%</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
</ol>

注意:在用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了。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn