JSP uploads images and automatically generates thumbnails

Source: Internet
Author: User

Jsp (preferred for SUN Enterprise Applications) uploads images and generates thumbnails

This example uses the jsp (preferred for SUN Enterprise Applications) smart component for uploading. Here, you can download this component www. jsp (preferred for SUN Enterprise Applications) smart.com for free.
After downloading and decompressing the package, copy the jar package to the WEB-INFlib directory and restart the server. jsp (preferred for SUN Enterprise Applications) smart can be used normally.

1. uploadimage. jsp (preferred for SUN Enterprise applications)

<% @ 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. jsp (preferred for SUN Enterprise Applications) smart. upload. *, java. util. *, cn. oof. database. *, cn. oof. house. * "%>
<%
SmartUpload mySmartUpload = new SmartUpload ();
Long file_size_max = 4000000;
String fileName2 = "", ext = "", testvar = "";
String url = "previusfile/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 (preferred for SUN Enterprise applications );
</Script>
<%
}
Try {

Com. jsp (preferred for SUN Enterprise Applications) smart. upload. File myFile = mySmartUpload. getFiles (). getFile (0 );
If (myFile. isMissing () {%>
<SCRIPT language = javascript>
Alert ("select the file to upload first ");
Window. location = upfile. jsp (preferred for SUN Enterprise applications );
</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.

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.