jsp中图片处理相关操作
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*"%>
<html>
<body>
<%
/*
drop table imagetable;
create table imagetable
(
nid int not null,
image blob,
Prima(最完善的虚拟主机管理系统)ry key (nid)
)
type = InnoDB;
*/
/*
//================ 一 、将文件写入到数据库的大字段中begin=====================
Class.forName("org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver").newInstance();
String url ="jdbc:MySQL(和PHP搭配之最佳组合)://localhost:3306/test?user=root&password=eastsoftweb";
Connection conn= DriverManager.getConnection(url);
java.io.File file = new File("d:/temp/1.jpg");
FileInputStream is=new FileInputStream(file);
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO imagetable (nid,image)" +
"VALUES (?, ?)"); //预编译SQL语句
stmt.setInt(1, 1);
stmt.setBinaryStream(2, is,(int)file.length());
stmt.executeUpdate();
stmt.close();
is.close();
out.println("update end");
//===============将文件写入到数据库的大字段中end=========================
*/
/*
//====================== 二、jsp(SUN企业级应用的首选)显示服务器硬盘图片示例 begin==============
FileInputStream is=new FileInputStream("d:/temp/1.JPG");
response.reset();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len=is.read(buffer))>0){
sos.write(buffer,0,len);
}
sos.flush();
sos.close();
//=======================jsp(SUN企业级应用的首选)显示服务器硬盘图片示例 end===================
*/
//===================== 三、将数据库的大字段图片还原到本地,并在网页上显示begin==============
Class.forName("org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver").newInstance();
String url ="jdbc:MySQL(和PHP搭配之最佳组合)://localhost:3306/test?user=root&password=eastsoftweb";
Connection conn= DriverManager.getConnection(url);
java.io.File file = new File("d:/temp/db.jpg");
FileOutputStream os=new FileOutputStream(file);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select nid,image from imagetable where nid=1");
rs.next();
byte[] buffer=rs.getBytes(2);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>