<% @ Page contentType = "text/html; charset = UTF-8" 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/"; // 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, bmp "); // Upload a file MySmartUpload. upload (); } Catch (Exception e ){ %> <SCRIPT language = javascript> Alert ("upload only .jpg).bmp and .gif image files allowed "); Registry.location='upload.htm '; </Script> <% } Try { Com. jspsmart. upload. File myFile = mySmartUpload. getFiles (). getFile (0 ); If (myFile. isMissing () {%> <SCRIPT language = javascript> Alert ("select the file to upload first "); Registry.location='upload.htm '; </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 String newpic = url + filename + "." + ext; 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 + "min _" + filename + "." + 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 (); Out. print ("<SCRIPT language = 'javascript '> "); Out. print ("alert ('file uploaded successfully ');"); Out. print ("parent.doc ument. form1.img. value = '" + newpic + "';"); Out. print ("your own locationnames 'upload.htm ';"); Out. print ("</SCRIPT> "); } Else { Out. print ("<SCRIPT language = 'javascript '> "); Out. print ("alert ('the size of the uploaded file cannot exceed '+ (file_size_max/1000) +" K ');"); Out. print ("your own locationnames 'upload.htm ';"); Out. print ("</SCRIPT> "); } } } Catch (Exception e ){ E. toString (); } %> |