jsp中圖片處理相關操作

來源:互聯網
上載者:User

<%@ 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================================
*/
%> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.