关于在JSP中图片的上传、正常显示、缩略显示
2009-02-17 18:39
411 查看
<%@ 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,
primary key (nid)
)
type = InnoDB;
*/
/*
//================ 一 、将文件写入到数据库的大字段中begin=====================
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://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显示服务器硬盘图片示例 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显示服务器硬盘图片示例 end===================
*/
//===================== 三、将数据库的大字段图片还原到本地,并在网页上显示begin==============
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://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);
stmt.close();
os.write(buffer);
os.flush();
os.close();
out.println("query end");
//网页上显示
response.reset();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
sos.write(buffer);
sos.flush();
sos.close();
//======================将数据库的大字段图片还原到本地,并在网页上显示end===================
/*
//======================四、生成缩略图begin==============================
File file = new File("d:/temp/1.JPG");
String newurl="d:/temp/2.jpg"; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=200;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0; //得到源图长
int tempsize;
float tempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
jsp中图片处理相关操作(2)
时间:2007-06-10 来源:开发者参考网(devtopic.com) 类别:Jsp
内容提要:FileOutputStream newimage=new FileOutputStream(newurl);牋牋牋牋?//输出到文件流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);牋牋牋 ...
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
//========================生成缩略图end================================
*/
%>
<%@ 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,
primary key (nid)
)
type = InnoDB;
*/
/*
//================ 一 、将文件写入到数据库的大字段中begin=====================
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://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显示服务器硬盘图片示例 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显示服务器硬盘图片示例 end===================
*/
//===================== 三、将数据库的大字段图片还原到本地,并在网页上显示begin==============
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://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);
stmt.close();
os.write(buffer);
os.flush();
os.close();
out.println("query end");
//网页上显示
response.reset();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
sos.write(buffer);
sos.flush();
sos.close();
//======================将数据库的大字段图片还原到本地,并在网页上显示end===================
/*
//======================四、生成缩略图begin==============================
File file = new File("d:/temp/1.JPG");
String newurl="d:/temp/2.jpg"; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=200;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0; //得到源图长
int tempsize;
float tempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
jsp中图片处理相关操作(2)
时间:2007-06-10 来源:开发者参考网(devtopic.com) 类别:Jsp
内容提要:FileOutputStream newimage=new FileOutputStream(newurl);牋牋牋牋?//输出到文件流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);牋牋牋 ...
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
//========================生成缩略图end================================
*/
%>
相关文章推荐
- 关于UEditor上传图片后显示不正常,路径错误的原因
- Django学习之路(九)关于图片的上传存储显示
- 关于web项目中的图片上传、并显示问题
- JSP页面,实现上传图片即时显示效果
- 求一段smartupload上传图片的代码,不需要保存到数据库,但是想在jsp页面选中文件夹图片后,立马可以显示
- 关于kindeditor编辑器在jsp中把图片上传路径改为绝对路径
- 百度编辑器本地上传图片可以正常显示,部署到服务器上就显示不出来只显示图片的名字
- JSP 上传图片+显示图片的问题
- JSP使用ckfinder实现中文图片上传后,无法显示,主要是tomcat不支持中文路径
- 关于jsp页面中显示中文图片问题
- jsp中图片的上传和显示
- js实现图片上传并正常显示
- jsp页面上传图片之后页面,当前页面的显示上传的图片可以自行更新
- 我在开发上传图片文件预览的时候IE无法正常显示,火狐可以正常显示
- 关于kindeditor编辑器批量上传图片不显示添加图片按钮的问题
- 解决图片路径中含有中文在jsp下不能正常显示问题
- 解决网站中上传的图片在IE中不显示 chrome firefox下却正常
- 关于ajaxfileupload.js一些问题和上传图片就立即显示图片功能
- struts2中使用Blob类型处理图片上传保存在数据库中并在JSP页面中显示图片
- jsp图片上传到数据库里面之后,不能直接在页面上显示,而是提示下载