JSP uploads images and generates thumbnails

Source: Internet
Author: User

In this example, the jspsmart component is used for uploading. You can download this component www.jspsmart.com for free.
Download unzip, copy the jar package to \ WEB-INF \ lib directory and restart the server, jspsmart can be used normally

1. uploadimage. jsp

 

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. io. *, java. awt. image, java. awt. image. *, com.sun.image.codec.jpeg. *, java. SQL. *, com. jspsmart. upload. *, java. util. *, cn. oof. database. *, cn. oof. house. * "%>
<%
SmartUpload mySmartUpload = new SmartUpload ();
Long file_size_max = 4000000;
String fileName2 = "", ext = "", testvar = "";
String url = "uploadfile/images/"; // ensure that this directory exists in the root directory
// Initialization
MySmartUpload. initialize (pageContext );
// Only files of this type can be uploaded
Try {
MySmartUpload. setAllowedFilesList ("jpg, gif ");
// Upload a file
MySmartUpload. upload ();
} Catch (Exception e ){
%>
<SCRIPT language = javascript>
Alert ("Upload only. jpg and. GIF image files allowed ");
Window. location = 'upfile. jsp ';
</Script>
<%
}
Try {

Com. jspsmart. upload. File myFile = mySmartUpload. getFiles (). getFile (0 );
If (myFile. isMissing () {%>
<SCRIPT language = javascript>
Alert ("select the file to upload first ");
Window. location = 'upfile. jsp ';
</Script>
<%}
Else {
// String myFileName = myFile. getFileName (); // get the name of the uploaded file
Ext = myFile. getFileExt (); // get the suffix
Int file_size = myFile. getSize (); // get the file size
String saveurl = "";
If (file_size <file_size_max ){
// Change the file name to obtain the millisecond value of the current upload time
Calendar calendar = Calendar. getInstance ();
String filename = String. valueOf (calendar. getTimeInMillis ());
Saveurl = request. getRealPath ("/") + url;
Saveurl + = filename + "." + ext; // save path
MyFile. saveAs (saveurl, mySmartUpload. SAVE_PHYSICAL );
// Out. print (filename );
// ----------------------- After the upload is complete, the thumbnail is generated -------------------------
Java. io. File file = new java. io. File (saveurl); // read the uploaded File
String newurl = request. getRealPath ("/") + url + filename + "_ min." + ext; // 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 ();

}
Else {
Out. print ("<SCRIPT language = 'javascript '> ");
Out. print ("alert ('the size of the uploaded file cannot exceed '+ (file_size_max/1000) +" K ');");
Out. print ("window. location = 'upfile. jsp ;'");
Out. print ("</SCRIPT> ");
}
}
} Catch (Exception e ){

E. toString ();

}
%>
 

2 upload.htm

 

<Html>
<Head>
<Title> select an uploaded image </title>
</Head>
<Body>
<Table border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<Td height = "45" align = "center" valign = "middle"> <form action = "uploadimage. jsp "method =" post "enctype =" multipart/form-data "name =" form1 ">
Select the uploaded Image
<Input type = "file" name = "file">
<Input type = "submit" name = "Submit" value = "Upload">
</Form> </td>
</Tr>
</Table>
</Body>
</Html>

Author: "Li Changchun's blog"
 

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.