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.