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=========================
*/
/*
//====================== 二、(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);