背景!jsp+mysql 记住 要用mysql的longblob类型来存默认的blob大小不够
数据库字段:id (char) pic (longblob)
转载请注明出处,这时我与我的知己的合作的结过
原来操作blob字段时都要先差个空值,在查blob,好麻烦,用prepareStatment就不用那么麻烦了,哈哈
postblob.heml页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="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" 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
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html xmlns="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.Driver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:mysql://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">查看图片</a>
<a href="postblob.html">返回</a>
</body>
</html>
********************************************************