Code for generating image thumbnails in jsp

Source: Internet
Author: User

<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page language = "java" import = "java. util. *" %>
<% @ Page language = "java" import = "java. io. *" %>
<% @ Page language = "java" import = "java. awt. *" %>
<% @ Page language = "java" import = "java. awt. image. *" %>
<% @ Page language = "java" import = "com.sun.image.codec.jpeg. *" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Meta name = "generator" content = "editplus"/>
<Meta name = "author" content = "Wang Shiqiang"/>
<Meta name = "keywords" content = "Wang Shiqiang"/>
<Meta name = "description" content = ""/>
</Head>

<Body>
<%
Response. setContentType ("text/html; charset = gb2312 ");
Request. setCharacterEncoding ("gb2312 ");

Out. print ("========================== Test for thumbtail picture ================ ");

// ----------------------- After the upload is complete, the thumbnail is generated -------------------------
Java. io. File file = new java. io. File ("E: \ 1110168154_58348.jpg"); // saveurl); // read the uploaded File
String newurl = "E: \ 1110168154_58348_min.jpg"; // request. getRealPath ("/") + url + filename + "_ min." + ext; // new thumbnail save address
Image src = javax. imageio. ImageIO. read (file); // construct an Image object
Float tagsize = 287;
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;
Out. print ("<br/> the old width is:" + old_w + "the old height is" + old_h + "<br/> ");
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.
Out. print ("the new width is:" + new_w + "the new height is" + new_h + "<br/> ");
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 ();
/*
*/
%>
</Body>
</Html>

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.