Simple jsp file upload program

Source: Internet
Author: User
Tags file upload
The code is as follows: Copy code

<Html>

<Title> Upload </title>

<Style type = "text/css">

Body {font: 12px Tahoma; font-family: ""; margin: 0px; background: # f3f8fb ;}

Body, form, ul, li, p, dl, dd, dt, h, td, th, h3 {padding: 0; font-size: 12px; color: #444 ;}

. Button {border: #7F9DB9 1px solid; height: 18px ;}

. Input {border: #7F9DB9 1px solid; height: 18px ;}

. Table {width: 250px ;}

</Style>

<Body>

<Form action = "uploadimage. jsp" method = "post" enctype = "multipart/form-data" name = "form">

<Input type = "file" name = "file">

<Input type = "submit" name = "Submit" value = "upload">

</Form>

</Body>

</Html>

Uploadimage. jsp file

The code is as follows: Copy code

<% @ 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 ();

}
%>

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.