Image processing operations in JSP

Source: Internet
Author: 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;

*/

/*
// ========================= 1. Write the file to the large field of the database and begin in ============ ==============
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 (?, ?) "); // Pre-compiled SQL statements
Stmt. setint (1, 1 );
Stmt. setbinarystream (2, is, (INT) file. Length ());
Stmt.exe cuteupdate ();
Stmt. Close ();
Is. Close ();
Out. println ("Update end ");
// ======================== Write the file into the large field of the Database End ==================== ================
*/

/*
// ======================================= 2. jsp display server hard disk image example 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 display server hard disk image example end ========== ==============
*/

 

// ===================================3. Restore the large field image of the database to the local machine, and show begin ====================== on the webpage
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 rs1_stmt.exe cutequery ("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 ");

// Displayed on the webpage
Response. Reset ();
Response. setcontenttype ("image/JPEG ");
Servletoutputstream SOS = response. getoutputstream ();
SOS. Write (buffer );
SOS. Flush ();
SOS. Close ();
// =========================================Restore the large field image of the database to the local machine, and show end ================================

 
/*
// =====================================4. Generate a thumbnail begin ================ ==============================
File file = new file ("D:/temp/1.jpg ");
String newurl = "D:/temp/2.jpg"; // new thumbnail storage address
Image src = javax. ImageIO. ImageIO. Read (File); // construct an image object
Float tagsize = 200;
Int old_w = SRC. getwidth (null); // obtain the source image width.
Int old_h = SRC. getheight (null );
Int new_w = 0;
Int new_h = 0; // obtain the source image length.
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); // calculate the length and width of the new graph.
Bufferedimage tag = new bufferedimage (new_w, new_h, bufferedimage. type_int_rgb );
Tag. getgraphics (). drawimage (SRC, new_w, new_h, null); // draw the reduced Graph
Fileoutputstream newimage = new fileoutputstream (newurl); // output to the file stream
Required imageencoder encoder = required codec. createjpegencoder (newimage );
Encoder. encode (TAG); // near JPEG encoding
Newimage. Close ();
// =========================================Generate a thumbnail end ================ ==================================
*/
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.