background! jsp+mysql Remember to use the longblob type of mysql to store the database fields. The default blob size is not enough
: id (char) pic (longblob)
Please indicate the source for reprinting. At this time, I cooperated with my confidant to complete
the original operation blob When the field, you must have a empty value. Check the BLOB, which is so troublesome. You do n’t need to be so troublesome to use the PEPAREPARESTATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATORESTATATATATORESTATOMEMEMEML
page
.
" 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>Untitled Document</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="Submit"/></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>Untitled Document</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">View image</a>
<a href="postblob.html">Return</a>
</body>
</html>
********************************************** **********
readblob.jsp
<%@ 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 xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<body>
<%
java.sql.Connection conn;
ResultSet rs=null;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:mysql://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>
*************************************************** ******************
Note: When using sos.write(by, 0, bytesRead);, this method outputs the content in the inputstream in a new page.
If there is other content to be output on this page, you can only change the above method to, bytesRead = in.read(by));
then use the out.print(new String(by)); method to output the result, pay attention here The by.toString() method cannot be used. This method returns the memory address of the content to be output. There are blob textarea types in mysql. The size is 66536. Basically, it is enough to put some small stuff, haha. But now that digital pics are getting bigger and bigger, you can only use longblob. The size is 4g, which is enough to play a movie. Haha.