欢迎光临
我们一直在努力

jsp中图片处理相关操作-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

<%@ 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);       //绘制缩小后的图
fileoutputstream newimage=new fileoutputstream(newurl);          //输出到文件流
jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);      
encoder.encode(tag);                                               //近jpeg编码
newimage.close();   
//========================生成缩略图end================================
*/
%>

a

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp中图片处理相关操作-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章