JSP如何读取MySql中MEDIUMBLOB字符串
来源:互联网 发布:c语言英航卡管理系统 编辑:程序博客网 时间:2024/05/01 09:36
JSP如何读取MySql中MEDIUMBLOB字符串,并显示?
实现向MYSQL数据库中存储或提取图片文件
一些情况下,需要向数据库中存储一些2进制文件,比如图片文件等,这时候,向数据库存储数据不同于普通的字符串存储,我们需要对这个2进制文件使用JAVA处理2进制流的API进行处理,然后再进行存储。我们需要进行以下步骤来实现:
向数据库中存储文件的时候,一样使用标准SQL语句,如: insert into database (column1, column2,..)values(v1,v2,…);注意的是,要在建立存放2进制文件的TABLE时,存放的字段要使用BLOB类型,而不是普通的VARCHAR等。BLOB是专门存储2进制文件的类型,他还有大小之分,比如mediablob,logblob等,以存储大小不同的2进制文件,一般的图形文件使用mediablob足以了。
1 见以下代码实现向MYSQL中储存图片文件:
…………………………
private final String insertquery = "insert into employeephoto(Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
public void doInsertStaffPic(String loginname,String source_URL) {
Connection conn = null;
PreparedStatement pre = null;
try {
// 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过JDBC直接连
conn = DBProcess.getConnection();
//从图片源中获得图片对象并写到缓存中
Image image = new ImageIcon(source_URL).getImage();
BufferedImage bImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
//将图片写入2进制的输出流 并放如到byte[] buf中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);
byte[] buf = out.toByteArray();
//获得这个输出流并将他设置到BLOB中
ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
pre = conn.prepareStatement(insertstaffpicquery);
pre.setString(1, loginname);
pre.setBinaryStream(2, inStream, inStream.available());
// 执行写如数据
pre.executeUpdate();
} catch (Exception exc) {
exc.printStackTrace();
}
finally {
try {
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:
…………………………
private final String writeoutquery = "insert into employeephoto(Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
// retrive the picture data from database and write it to the local disk
public void doGetAndShowStaffPic(String loginname, String dir) {
FileOutputStream output = null;
InputStream input = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pre = null;
try {
conn = DBProcess.getConnection();
pre = conn.prepareStatement(writeoutquery);
pre.setString(1, loginname);
rs = pre.executeQuery();
if (rs.next()) {
// 从数据库获得2进制文件数据
Blob image = rs.getBlob("Binary_Photo");
// setup the streams
Input = image.getBinaryStream();
try {
// 设置写出路径。
output = new FileOutputStream(dir);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// set read buffer size 注意不要设置的太小,要是太小,图片可能不完整
byte[] rb = new byte[1024000];
int ch = 0;
// process blob
try {
// 写入本地文件系统
while ((ch = input.read(rb)) != -1) {
output.write(rb, 0, ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- JSP如何读取MySql中MEDIUMBLOB字符串
- MySql MediumBlob
- jsp中如何读取一个XML文件
- 如何从一串字符串中读取数字
- MySql MediumBlob——MySql的Bolb四种类型
- Jsp读取Mysql数据库
- 如何在jsp中读取配置文件的内容
- 如何从输入流中读取数据到字符串中
- 在java中如何从键盘中读取一个字符串
- jsp中读取cookie
- MySql 中如何连接一列字符串
- jsp mysql 插入 读取 图片
- WebService如何从Web.Config中读取数据库连接字符串
- C#中如何读取webconfig.xml里的数据库连接字符串
- 如何从文件中读取字符串到string对象
- 如何将数据库连接字符串保存在配置文件中并读取
- C++中字符串读取
- PHP中如何连接Mysql以及读取数据
- 10个新的最有前途的JavaScript框架
- 搜索引擎测试方法
- C++ 过滤特殊字符
- List的contains()方法
- 解决乱码问题
- JSP如何读取MySql中MEDIUMBLOB字符串
- apache 基于IP、port和域名的三种虚拟主机的配置方法
- 面向对象(一)
- 调查:多数公众认可MID取代手机
- 面向对象(二)
- Eclipse3.4中CVS服务器配置中出现的问题
- php获取mysql数据库信息
- tomcat6.0 调用serverlet时显示 404错误解决的方法
- 商务人士专用 Intel开发多屏拼接技术