jsp中操作blob经验谈及在页面中的实际情况
2006-03-25 16:41
567 查看
背景!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>
********************************************************
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>无标题文档</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>
********************************************************************
注意:在用sos.write(by, 0, bytesRead);时,该方法把inputstream中的内容在一个新的页面中输出,
如果本页中还有别的内容要输出的话,只有把上述方法改为,bytesRead = in.read(by)) ;
再用out.print(new String(by));方法输出结果,注意在这里不能用by.toString()方法,该方法返回的是要输出内容的内存地址。mysql中有blob textarea类型大小了66536基本上放点小的东东就足够了,哈哈,但是现在的数码pic越来越大就只能用longblob了 大小有4g能 ,放个电影都够了哈哈
数据库字段: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>
********************************************************
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>无标题文档</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>
********************************************************************
注意:在用sos.write(by, 0, bytesRead);时,该方法把inputstream中的内容在一个新的页面中输出,
如果本页中还有别的内容要输出的话,只有把上述方法改为,bytesRead = in.read(by)) ;
再用out.print(new String(by));方法输出结果,注意在这里不能用by.toString()方法,该方法返回的是要输出内容的内存地址。mysql中有blob textarea类型大小了66536基本上放点小的东东就足够了,哈哈,但是现在的数码pic越来越大就只能用longblob了 大小有4g能 ,放个电影都够了哈哈
相关文章推荐
- 解决浏览器,servlet,jsp页面之间传值出现乱码的情况
- 从数据库取出数据在jsp页面上以表格形式呈现,并对表格数据进行分页打印操作。
- 基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作
- JSP 页面中对Cookie的操作
- 基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作
- JSP页面href跳转情况
- CSDN日报20170427 ——《如何在没有实际项目经验的情况下找到工作》
- JSP页面批量选择&全选操作&选择回显
- SSH中JSP页面传值到action出现中文乱码的情况
- 基于BootStrap Metronic开发框架经验小结【九】实现Web页面内容的打印预览和保存操作
- JSP页面之“IE无法打开Internet 站点…… 已终止操作”问题
- jstl标签在jsp页面对字符串的操作
- 制作login.jsp页面,要求实现提供用户登陆的界面, 用户通过该界面输入用户名(控件名userName)和密码(控件名password), 然后点击“登录”按钮实现登陆操作.
- jsp页面input框中值或事件的操作方式
- Hibernate 对数据库Blob图片的查询操作---页面显示图片,文件的输出
- WINAPI 区域的建立与删除实际操作注意情况
- JSP页面同时操作所有Input输入框
- 防用户不登陆操作直接请求jsp页面
- JSP读取blob到页面上
- 教大家在Jsp页面中用el表达式对变量进行操作